关于数据结构的创建列表,输出列表,插入列表,和撤除列表。我的编译通过了,但是有一个警告。而且得不到结果,估计是创建列表有问题。但

uglykoala 2003-09-14 01:25:35
程序内容如下:#define NULL 0
#define LEN sizeof(struct student)
struct student
{long num;
float score;
struct student *next;
};
int n;
struct student *creat(void)
{struct student *head,*p1,*p2;
n=0;
head=NULL;
p1=(struct student *)malloc(LEN);
p2=p1;
scanf("%ld,%f",&p1->num,&p1->score);
while(p1->num!=0)
{n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
void print (struct student *head)
{struct student *p;
printf("\n now ,these %d records are :\n",n);
p=head;
if(head!=NULL)
do
{printf("%ld,%5.1f\n",p->num,p->score);
p=p->next;
}
while(p!=NULL);
}
struct student *del(struct student *head,long num)
{struct student *p1,*p2;
p1=head;
while(p1->num!=num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->num==num)
{if(p1==head)
head=p1->next;
else
p2->next=p1->next;
printf("delete :%ld\n",num);
n=n-1;
}
else
printf("%ld not been found!\n",num);
return(head);
}
struct student *insert(struct student *head,struct student *stu)
{struct student *p0,*p1,*p2;
p1=head;
p0=stu;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
{while((p0->num>p1->num)&&(p1->next!=NULL))
{p2=p1;p1=p1->next;}
if(p0->num<p1->num)
{if(p1==head)
{head=p0;p0->next=p1;}
else
{p2=p0;p0->next=p1;}
}
else
{p1->next=p0;p0->next=NULL;}
}
n=n+1;
return(head);
}
main()
{struct student *head,*stu;
long del_num;
printf("input records:\n");
head=creat();
print(head);
printf("\ninput the deleted number:");
scanf("%ld",&del_num);
while(del_num!=0)
{head=del(head,del_num);
print(head);
printf("input the deleted number:");
scanf("%ld",&del_num);
}
printf("\ninput the inserted record:");
stu=(struct student *)malloc(LEN);
scanf("%ld,%f",&stu->num,&stu->score);
while(stu->num!=0)
{head=insert(head,stu);
print(head);
printf("input the inserted record:");
stu=(struct student *)malloc(LEN);
scanf("%ld,%f",&stu->num,&stu->score);
}
}

...全文
47 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
zilin82 2003-09-19
  • 打赏
  • 举报
回复
你可以在浏览器的地址栏输入ftp.cppcn.com即可
uglykoala 2003-09-18
  • 打赏
  • 举报
回复
见笑了。
我还没有用过ftp 你可不可以告诉我用的方法呀??
uglykoala 2003-09-17
  • 打赏
  • 举报
回复
用了。还是不行。你们用的是什么版本。告诉我在哪里下载。谢谢了。
zilin82 2003-09-17
  • 打赏
  • 举报
回复
是我自己的盘里的,我推荐你在ftp.cppcn.com这个ftp服务器里有,你可以用匿名用户进去
zilin82 2003-09-16
  • 打赏
  • 举报
回复
还有是就是有两个学号一样那也无妨,插入时没问题,只是删除时只能一个一个的删除而已,还有楼上的那个插入法和我的那个插入法是一样的,不比你的复杂,只是你是以退出while语句
p0->num<p1->num为依据,而我是以p1->next是否为0依据而已
zilin82 2003-09-16
  • 打赏
  • 举报
回复
你没有用我的程序试一下吗,我编译都没问题,学号应该不会相同的,在实际中的主关键码也应该是不同,否则那就还有辅助关键嘛
uglykoala 2003-09-15
  • 打赏
  • 举报
回复
hi zilin82(逍遥游)你好。
你说的很对。其实我的插入函数有一个东西写掉了。就造成了你说的错误。
非常感谢。不过我觉得没有必要写的向你那么复杂。
struct student *insert(struct student *head,struct student *stu)
{struct student *p0,*p1,*p2;
p1=head;
p0=stu;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
{while((p0->num>p1->num)&&(p1->next!=NULL))
{p2=p1;p1=p1->next;}
if(p0->num<p1->num)
{if(p1==head)
{head=p0;p0->next=p1;}
else
{p2=p0;p0->next=p1;} /*这个地方改改,改成{p2->next=p0;p0->next=p1;}*/
}
else
{p1->next=p0;p0->next=NULL;}
}
n=n+1;
return(head);
}
这一次编译成功了。可是结果还是一样的。难道是我的电脑出了问题吗?
你们可不可以在TC2。0下试一试。
TianGuangZao 2003-09-15
  • 打赏
  • 举报
回复
还有个比较重要的问题,如果学号一样,就比较讨厌了,得重新设计算法了。
zilin82 2003-09-15
  • 打赏
  • 举报
回复
我上面是在vc6.0运行的,我想在tc下也应该没问题
zilin82 2003-09-15
  • 打赏
  • 举报
回复
我在修改前,我运行的结果是:
input records:
1,10
input records:
2,11
input records:
3,12
input records:
4,13
input records:
5,14
input records:
0,0

now ,these 5 records are :
1, 10.0
2, 11.0
3, 12.0
4, 13.0
5, 14.0

input the deleted number:3
delete :3

now ,these 4 records are :
1, 10.0
2, 11.0
4, 13.0
5, 14.0
input the deleted number:0

input the inserted record:3

now ,these 5 records are :
1, 10.0
2, 11.0
4, 13.0
5, 14.0//这里有问题,是插入函数有问题,在中间插入时就不能插入
input the inserted record:6

now ,these 6 records are :
1, 10.0
2, 11.0
4, 13.0
5, 14.0
6, 15.0//但是在后面插入就可以
input the inserted record:
我把插入函数修改了一下:
#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{long num;
float score;
struct student *next;
};
int n;
struct student *creat(void)
{struct student *head,*p1,*p2;
n=0;
head=NULL;
p1=(struct student *)malloc(LEN);
p2=p1;
scanf("%ld,%f",&p1->num,&p1->score);
while(p1->num!=0)
{n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
printf("input records:\n");
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
void print (struct student *head)
{struct student *p;
printf("\n now ,these %d records are :\n",n);
p=head;
if(head!=NULL)
do
{printf("%ld,%5.1f\n",p->num,p->score);
p=p->next;
}
while(p!=NULL);
}
struct student *del(struct student *head,long num)
{struct student *p1,*p2;
p1=head;
while(p1->num!=num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->num==num)
{if(p1==head)
head=p1->next;
else
p2->next=p1->next;
printf("delete :%ld\n",num);
n=n-1;
}
else
printf("%ld not been found!\n",num);
return(head);
}
struct student *insert(struct student *head,struct student *stu)//修改部分
{
struct student *p0,*p1,*p2;
p1=head;
p0=stu;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
{
while((p0->num>p1->num)&&(p1->next!=NULL))
{p2=p1;p1=p1->next;}//这个循环退出后p1->next=0或是p0->num<p1->num,
//所以当p1->next!=0时就把p0插入到p2后
if(p1->next==0)
{
if(p0->num<p1->num)
{head=p0;p0->next=p1;}
else
{p0->next=p1->next;p1->next=p0;}
}
else
{
p0->next=p1;
p2->next=p0;
}
}
n=n+1;
return(head);
}
void main()
{struct student *head,*stu;
long del_num;
printf("input records:\n");
head=creat();
print(head);
printf("\ninput the deleted number:");
scanf("%ld",&del_num);
while(del_num!=0)
{head=del(head,del_num);
print(head);
printf("input the deleted number:");
scanf("%ld",&del_num);
}
printf("\ninput the inserted record:");
stu=(struct student *)malloc(LEN);
scanf("%ld,%f",&stu->num,&stu->score);
while(stu->num!=0)
{head=insert(head,stu);
print(head);
printf("input the inserted record:");
stu=(struct student *)malloc(LEN);
scanf("%ld,%f",&stu->num,&stu->score);
}
}
我改过了了你可以了结果是:
input records:
1,10
input records:
2,11
input records:
3,12
input records:
4,13
input records:
5,14
input records:
0,0

now ,these 5 records are :
1, 10.0
2, 11.0
3, 12.0
4, 13.0
5, 14.0

input the deleted number:3
delete :3

now ,these 4 records are :
1, 10.0
2, 11.0
4, 13.0
5, 14.0
input the deleted number:0

input the inserted record:3,12

now ,these 5 records are :
1, 10.0
2, 11.0
3, 12.0
4, 13.0
5, 14.0
input the inserted record:6,15

now ,these 6 records are :
1, 10.0
2, 11.0
3, 12.0
4, 13.0
5, 14.0
6, 15.0
input the inserted record:0,0
Press any key to continue
uglykoala 2003-09-15
  • 打赏
  • 举报
回复
呵呵。我说的/ 表示回车。
呵呵。我太马虎了。呵呵
TianGuangZao 2003-09-15
  • 打赏
  • 举报
回复
对,老兄考虑到了,我没仔细看。
TC 我没用过,windows 好久没用过了,没法在它上面帮你测试。
1,100/
我没看懂,我一般都是 1,100 中间没空格,也不带 /
uglykoala 2003-09-14
  • 打赏
  • 举报
回复
你说的问题很有道理,我还没有注意到,谢谢你。可是我的问题好象并没有解决。
我觉得我的问题是出在创建列表的阶段。
uglykoala 2003-09-14
  • 打赏
  • 举报
回复
但是我在TC 2。0里不能向预想的那样输入。
我输入1,100/
结果成了:scnaf:floating point formats not linked
Abnormal program termination
就你说的第二个问题,
printf("input the inserted record:");
stu=(struct student *)malloc(LEN);
scanf("%ld,%f",&stu->num,&stu->score);
}
这一段,只能输入,不能结束输入不是很好。
我的预想是输入0,0/
输入结束;

TianGuangZao 2003-09-14
  • 打赏
  • 举报
回复
我是这样输入数据的:
1,100
2,98
3,67
。。。
按你得算法得十分谨慎输入,所以这里得多改改,考虑得全面些。
还有
printf("input the inserted record:");
stu=(struct student *)malloc(LEN);
scanf("%ld,%f",&stu->num,&stu->score);
}
这一段,只能输入,不能结束输入不是很好。
我是在 gcc 下编译的,编译算是顺利,运行起来就碰到上述的几点,还没全面测试。
TianGuangZao 2003-09-14
  • 打赏
  • 举报
回复
好多问题,如没检查非法数据。还有结构上感觉比较乱。
帮你修改了一处,好让程序能下去,其它地方自己慢慢改吧!
main()
{struct student *head,*stu;
long del_num;
printf("input records:\n");
head=creat();
print(head);
printf("\ninput the deleted number:");
scanf("%ld",&del_num);
while(del_num!=0 && n > 1) // 这里加上 && n > 1
{head=del(head,del_num);
print(head);
printf("input the deleted number:");
scanf("%ld",&del_num);
}
printf("\ninput the inserted record:");
stu=(struct student *)malloc(LEN);
scanf("%ld,%f",&stu->num,&stu->score);
while(stu->num!=0)
{head=insert(head,stu);
print(head);
printf("input the inserted record:");
stu=(struct student *)malloc(LEN);
scanf("%ld,%f",&stu->num,&stu->score);
}
}

69,335

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧