100分,帮我找毛病!机会不要错过!
下面是关于单链表的操作,帮我改一下,最好解释一下为什么?
#include "iostream.h "
#include "malloc.h "
#define ok 1
#define error 0
typedef struct LNode
{
int data;
LNode *next;
}LNode,*linklist;
void create_(linklist &l,int n)
{
linklist p;
int i;
l=new LNode;
l-> next=NULL;
while(p)
{
for(i=n;i> 0;i--)
{
p=new LNode;
cin> > p-> data;
p-> next=l-> next;
l-> next=p;
p=p-> next;
return ;
}
}
}
void print_(linklist l)
{
linklist p;
p=l-> next;
while(p)
{
cout < <p-> data;
p=p-> next;
}
}
int find_(linklist l,int i,int e)
{
linklist p;
p=l-> next;
int j=1;
while(!p||j> i) return 0;
while(p&&j <i)
{
p=p-> next;
++j;
}
e=p-> data;
return ok;
}
int insert_(linklist l,int i,int n)
{
linklist p,s;
p=l-> next;
int j=1;
while(!p||j> i-1) return error;
while(p&&j <i-1)
{
p=p-> next;
++j;
}
s=new LNode;
while(s==NULL) return error;
cin> > s-> data;
s-> next=l-> next;
l-> next=s;
return ok;
}
int delete_(linklist l,int i)
{
int j;
linklist p,q;
p=l-> next;
j=1;
while(!p||j> i-1) return error;
while(p&&j <i-1)
{
p=p-> next;
++j;
}
q=new LNode;
if(q==NULL) return error;
q=p-> next;
p-> next=q-> next;
free(q);
return ok;
}
void showmainmeu()
{
cout < < "功能如下: ";
cout < < "/************************************************************/ " < <endl;
cout < < "时间:2007-08-26 " < <endl;
cout < < "|*******************************| " < <endl;
cout < < "| ******1:创建链表 ******** | " < <endl;
cout < < "| ******2:输出链表 ******** | " < <endl;
cout < < "| ******3:查找链表 ********* | " < <endl;
cout < < "| ******4:插入链表 ******** | " < <endl;
cout < < "| ******5:删除链表 ******** | " < <endl;
cout < < "| ******6:返回系统 ******* | " < <endl;
}
void main()
{
linklist l;
int choice;
int n,i;
int e;
cout < < "请输入你要建立链表的个数 ";
cin> > n;
create_(l,n);
showmainmeu();
cout < < "输入你的功能序号 ";
cin> > choice;
while(choice!=6)
{
switch(choice)
{
case 1:
{cin> > "输入你要创建链表的个数 ";
create_(l,n);
break;
}
case 2:{
cout < < "逆序输出的链表为 ";
print_(l);
break;
}
case 3:
{
cout < < "输入你要查找的位置 ";
cin> > i;
find_(l,i,e);
cout < < "你要查找的元素为 ";
break;
}
case 4:
{
cout < < "输入你要插入的位置 ";
cin> > i;
cout < < "输入你要插入的元素 ";
cin> > n;
insert_(l,i,n);
break;
}
case 5:
{
cout < < "输入你要删除的位置 ";
cin> > i;
delete_(l,i);
break;
}
default :break;
}
cout < <endl;
cout < < "输入功能序列 ";
cin> > choice;
}
}
另外:我想问一下,关于return 的作用,它就是返回调用的地方,不用return,也会调用啊,是不是加了,就速度快一点,还是?还有return 0与return 1,或者return -1的意义没有区别吧?
[解决办法]
没仔细看但是首先用c书写代码尽量使用标准c库
而不要c++ 和c的搞在一起
风格很不好
return用在最后,应该不会加快速度都是退出栈 只是寄存器的加减PoP
return 0与return 1,或者return -1这个返回值是c语言常用的异常判断的一种方法
不同的返回值对应不同的result
[解决办法]
void create_(linklist &l,int n)
{
linklist p,q;//增加变量q
int i;
l=new LNode;
l-> next=NULL;
q=l; //初始化q
// while(p) 这个循环没用,而且p没初始化会出现错误
// {
for(i=n;i> 0;i--)
{
p=new LNode;
cin> > p-> data;
p-> next=NULL;//改为NULL,p-> next=l-> next;在第一次循环没错误,以后的就错了
q-> next=p; //并且返回的l
q=p; //将q始终指向链表的最后一个元素
p=p-> next; //为下一循环作准备
// return ; 这句去掉,有可能跳出循环
}
// }
}
void print_(linklist l)
{
linklist p;
//p=l-> next;
p=l;//这样才能打出第一个数字
while(p)
{
cout < <p-> data;
p=p-> next;
}
}
[解决办法]
楼主兄弟,你可能是学的时间不久是吧?
我说的话你不要生气,你的程序错误很多,而且很多都是低级错误,写好程序后要仔细检查一遍,养成良好的编程风格,我也记不清改了多少错误,不过你是新人,错误没有关系,继续努力,程序我已经帮你改好了,希望你好好看看,可能对你刚刚学有很大帮助!
#include "iostream.h "
#include "malloc.h "
#define ok 1
#define error 0
typedef struct LNode
{
int data;
LNode *next;
}LNode,*linklist;
linklist create_()
{
linklist p,l;
int i;
int n;
cout < < "请输入你要建立链表的个数: ";
cin> > n;
l=new LNode;
l-> next=NULL;
if(p)
for(i=n;i> 0;i--)
{
p=new LNode;
cin> > p-> data;
p-> next=l-> next;
l-> next=p;
// p=p-> next;
}
return l;
}
void print_(linklist l)
{
linklist p;
p=l-> next;
while(p)
{
cout < <p-> data;
p=p-> next;
}
}
int find_(linklist l,int i)
{
linklist p;
p=l-> next;
int j=1;
if(!p||j> i) return 0;
while(p&&j <i)
{
p=p-> next;
++j;
}
return p-> data;
}
int insert_(linklist l,int i,int n)
{
linklist p,s;
p=l-> next;
int j=1;
if(!p||j> i-1) return error;
while(p&&j <i-1)
{
p=p-> next;
++j;
}
s=new LNode;
if(s==NULL) return error;
s-> data = n;
s-> next=p-> next;
p-> next=s;
return ok;
}
int delete_(linklist l,int i)
{
int j;
linklist p,q;
p=l-> next;
j=1;
if(!p||j> i-1) return error;
while(p&&j <i-1)
{
p=p-> next;
++j;
}
q=p-> next;
p-> next=q-> next;
free(q);
return ok;
}
void showmainmeu()
{
cout < < "功能如下: ";
cout < < "/************************************************************/ " < <endl;
cout < < "时间:2007-08-26 " < <endl;
cout < < "|*******************************| " < <endl;
cout < < "| ******1:创建链表 ******** | " < <endl;
cout < < "| ******2:输出链表 ******** | " < <endl;
cout < < "| ******3:查找链表 ********* | " < <endl;
cout < < "| ******4:插入链表 ******** | " < <endl;
cout < < "| ******5:删除链表 ******** | " < <endl;
cout < < "| ******6:返回系统 ******* | " < <endl;
}
void main()
{
linklist hear;
int choice;
int n,i;
int e;
hear = create_();
showmainmeu();
cout < < "输入你的功能序号: ";
cin> > choice;
while(choice != 5)
{
while(choice > 5)
{
cout < < "输入无效,请重新输入: ";
cin> > choice;
}
if(choice==5) break;
switch(choice)
{
case 1:{
cout < < "逆序输出的链表为: ";
print_(hear);
cout < <endl;
break;
}
case 2:
{
cout < < "输入你要查找的位置: ";
cin> > i;
e = find_(hear,i);
cout < < "你要查找的元素为: " < <e < <endl;;
break;
}
case 3:
{
cout < < "输入你要插入的位置: ";
cin> > i;
cout < < "输入你要插入的元素: ";
cin> > n;
insert_(hear,i,n);
break;
}
case 4:
{
cout < < "输入你要删除的位置: ";
cin> > i;
delete_(hear,i);
break;
}
default :break;
}
// cout < <endl;
cout < < "输入功能序列: ";
cin> > choice;
}
}
以上的程序是正确的,我已经调试过了!
关于return ,在调用的时候如果需要有返回值的时候起作用,在不需要的时候加上return只是代表一个函数的结束,有的时候是为了返回一个标志,表明函数调用成功!
加油吧,兄弟!!