| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 623 人关注过本帖
标题:[求助]新手的链表问题?????
只看楼主 加入收藏
小林
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2006-9-7
收藏
 问题点数:0 回复次数:9 
[求助]新手的链表问题?????
这是插入函数,当插入第一个数时可以正常输出,插入第二个时就不行了,现象是无限第二个数???请问应该怎么改???? 下面的是全部程序.
/*琏表的综合操作*/
#include<stdio.h>
#include<stdlib.h>
struct student
{
int num;
struct student *next;
};
struct student *creat() /*创建琏表的函数*/
{
struct student *head,*p1,*p2;
p1=(struct student*)malloc(sizeof(struct student));
scanf("%d",&(p1->num));
head=p1;
while(p1->num!=0)
{
p2=(struct student*)malloc(sizeof(struct student));
p1->next=p2;
scanf("%d",&(p2->num));
p1=p2;
}
p1->next=NULL;
return(head);
}
void print(struct student *head) /*对琏表进行输出的函数*/
{
struct student *p;
p=head;
if(p==NULL)
printf("no link");
else
while(p->num!=0)
{
printf("%5d",p->num);
printf("\n");
p=p->next;
}
}
mydelete(struct student *head) /*删除琏表中某一结点的函数*/
{
struct student *p,*p1,*p2;
int n;
printf("which element you will delete\n");
scanf("%d",&n);
p=head;p1=head->next;p2=p1->next;
while(p2!=NULL);
{
p=p->next;
p1=p1->next;
p2=p2->next;
if(p1->num==n);
{
p2=p->next;
free(p1);
return 1;
}
}
return 0;
}
insert(struct student *head) /*对琏表的末尾插入一个元素*/
{
struct student *p2,*p,*p1,*p3;
int n;
p2=head->next,p=head,p1=head->next,p3=head;
printf("input the element\n");
scanf("%d",&n);
while(p!=NULL)
{
if(p->num==n) /*判断琏表中是否有与要插入的元素相同的结点*/
return 0;
p=p->next;
}
while(p1->num!=NULL)
{
p1=p1->next;p3=p3->next;
}
p2=(struct student*)malloc(sizeof(struct student));
p2=p3->next;
p2->num=n; /*当没有相同元素时,在末尾插入元素*/
p2->next=NULL;
return 1;
}
main()
{
struct student * head;
int h,k;
char choose;
k=10;
printf("C--creat link,D--delete element,I--insert link,P--print link,O--out\n");
for(k=0;k<=10;k++)
{
printf("choose...\n");
scanf("%c",&choose);
switch(choose) /*选择对琏表的操作*/
{
case'c': /*创建并打印琏表*/
{
printf("enter element\n");
head=creat();
printf("the link is\n");
print(head);
}
break;
case 'd':
{
h=mydelete(head);
if(h==1)
{
printf("the new link\n");
print(head);
}
if(h==0)
printf("Not Found The Element");
}
break;
case 'i':
{
h=insert(head);
if(h==1)
{
printf("new link is\n");
print(head);
}
if(h==0)
printf("link have had the element\n");
}
break;
case 'p':
print(head);
break;
default:
printf("error again\n");
}
if(choose=='o')
break;
}
getch();
}
搜索更多相关主题的帖子: 链表 
2006-09-08 11:49
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 
以下是引用小林在2006-9-8 11:49:06的发言:
这是插入函数,当插入第一个数时可以正常输出,插入第二个时就不行了,现象是无限第二个数???请问应该怎么改???? 下面的是全部程序.
/*琏表的综合操作*/
#include<stdio.h>
#include<stdlib.h>
struct student
{
int num;
struct student *next;
};
struct student *creat() /*创建琏表的函数*/
{
struct student *head,*p1,*p2;
p1=(struct student*)malloc(sizeof(struct student));
scanf("%d",&(p1->num));
head=p1;
while(p1->num!=0)
{
p2=(struct student*)malloc(sizeof(struct student));
p1->next=p2;
scanf("%d",&(p2->num));
p1=p2;
}
p1->next=NULL;
return(head);
}
void print(struct student *head) /*对琏表进行输出的函数*/
{
struct student *p;
p=head;
if(p==NULL)
printf("no link");
else
while(p->num!=0)//p!=NULL
{
printf("%5d",p->num);
printf("\n");
p=p->next;
}
}
mydelete(struct student *head) /*删除琏表中某一结点的函数*/
{
struct student *p,*p1,*p2;
int n;
printf("which element you will delete\n");
scanf("%d",&n);
p=head;p1=head->next;p2=p1->next;
while(p2!=NULL);
{
p=p->next;
p1=p1->next;
p2=p2->next;
if(p1->num==n);
{
p2=p->next;
free(p1);
return 1;
}
}
return 0;
}
insert(struct student *head) /*对琏表的末尾插入一个元素*/
{
struct student *p2,*p,*p1,*p3;
int n;
p2=head->next,p=head,p1=head->next,p3=head;
printf("input the element\n");
scanf("%d",&n);
while(p!=NULL)
{
if(p->num==n) /*判断琏表中是否有与要插入的元素相同的结点*/
return 0;
p=p->next;
}
while(p1->num!=NULL)
{
p1=p1->next;p3=p3->next;
}
p2=(struct student*)malloc(sizeof(struct student));
p2=p3->next;
p2->num=n; /*当没有相同元素时,在末尾插入元素*/
p2->next=NULL;
return 1;
}
main()
{
struct student * head;
int h,k;
char choose;
k=10;
printf("C--creat link,D--delete element,I--insert link,P--print link,O--out\n");
for(k=0;k<=10;k++)
{
printf("choose...\n");
scanf("%c",&choose);
switch(choose) /*选择对琏表的操作*/
{
case'c': /*创建并打印琏表*/
{
printf("enter element\n");
head=creat();
printf("the link is\n");
print(head);
}
break;
case 'd':
{
h=mydelete(head);
if(h==1)
{
printf("the new link\n");
print(head);
}
if(h==0)
printf("Not Found The Element");
}
break;
case 'i':
{
h=insert(head);
if(h==1)
{
printf("new link is\n");
print(head);
}
if(h==0)
printf("link have had the element\n");
}
break;
case 'p':
print(head);
break;
default:
printf("error again\n");
}
if(choose=='o')
break;
}
getch();
}

说实话,感觉很乱...我看的头痛


倚天照海花无数,流水高山心自知。
2006-09-08 12:44
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 

student *creat() /*创建琏表的函数*/
{
struct student *head,*p1,*p2;
int x;
scanf("%d",&x);

head=NULL;
p1=head;
while(x!=0)
{
p2=(struct student*)malloc(sizeof(struct student));
p2->num=x;
if(head==NULL)
{
head=p2;
}
else
{
p1->next=p2;
}
p1=p2;
scanf("%d",&x);

}
p1->next=NULL;
return(head);
}
void print(struct student *head) /*对琏表进行输出的函数*/
{
struct student *p;
p=head;
if(p==NULL)
{
printf("no link");
}
else
{
while(p!=NULL)
{
printf("%5d\n",p->num);
p=p->next;
}
}
}
int mydelete(struct student *head) /*删除琏表中某一结点的函数*/
{
struct student *pre,*p;
int n;
printf("which element you will delete\n");
scanf("%d",&n);

while(p!=NULL&&p->num!=n);
{
pre=p;
p=p->next;
}
if(p!=NULL)
{
if(p==head) //删除头结点
{
head=head->next;
}
else
{
pre->next=p->next;
}
free(p);
return(1);
}
else
{
return 0;
}
}
int insert(struct student *head) /*对琏表的末尾插入一个元素*/
{
struct student *p,*p1;
int n;
p=head;
printf("input the element\n");
scanf("%d",&n);
while(p->next!=NULL)
{
if(p->num==n) /*判断琏表中是否有与要插入的元素相同的结点*/
{
return 0;
}
p=p->next;
}
/*当没有相同元素时,在末尾插入元素*/
p1=(struct student*)malloc(sizeof(struct student));
p1->num=n;
p1->next=p->next;
p->next=p1;
return 1;
}

[此贴子已经被作者于2006-9-8 13:27:42编辑过]


倚天照海花无数,流水高山心自知。
2006-09-08 13:21
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 

里面的return(1),return(0)很不好.
你的主函数也不是很好,假如一刚开始不输入C,你说其他的操作有没有意义.
同时如果你修改了头结点,却在程序中没有重新返回头结点,这个也会导致错误的.
我在你的基础写的也没有考虑这些问题,反正就是两个字:头痛


倚天照海花无数,流水高山心自知。
2006-09-08 13:34
suiyugang
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2006-5-25
收藏
得分:0 
o  还没有学到链表呢,
2006-09-08 14:19
小林
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2006-9-7
收藏
得分:0 
问一下,为了更好的进行操作,都节点最好不要给负值,而是要设为空吗???

2006-09-10 10:29
小林
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2006-9-7
收藏
得分:0 
谢谢给的建议!

2006-09-10 10:30
小林
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2006-9-7
收藏
得分:0 
头结点是要付值为空吗????

2006-09-10 10:40
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 
恩,直接拿出一个结点做头结点(关键码为空也无所谓,因为不会用到该结点),链表的的操作不牵涉到头结点,头结点只是作为找链表的地址的.

你上面的删除函数如果删掉头结点,又不重新返回,这样链表就会游离,而占用内存.

所以用链表建议用带头结点的,方面,还实惠

倚天照海花无数,流水高山心自知。
2006-09-10 12:17
小林
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2006-9-7
收藏
得分:0 
嘿嘿,是由于我的编程习惯不好,斑竹伤眼睛了吧,下次一定改正!!

2006-09-20 12:07
快速回复:[求助]新手的链表问题?????
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015598 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved