为什么插入结点时候造成死循环啊?
我自己写的一个简单图书管理系统,课程设计作业.
可是问题出在add_book函数,为什么无限插入数目了.实在没找到原因.
大家帮帮忙,着急.....
另外,抱歉我只有这么多分呐
#include <stdio.h>
#include <stdlib.h>
struct book
{
int id;
char name[30],author[20];
struct book *next;
}books;
struct book *print_book(struct book *h)
{
struct book *p=h;
if(p)
{
printf( "no books\n ");
}
else
{
while (p)
{
printf( "book info:\n%d,%s,%s\n ",p-> id,p-> name,p-> author);
p=p-> next;
}
}
getche();
return h;
}
struct book *add_book()
{
struct book *p_front,*p_after,*h=NULL;
int n,i=0;
printf( "how many books you want to add: ");/*输入要添加的书的数量*/
scanf( "%d ",&n);
p_after=p_front=(struct book *)malloc(sizeof(struct book));
while(i <=n)
{
i=i+1;
printf( "No.%d\n ",i);
printf( "please input book id: ");
scanf( "%d ",&p_after-> id);
printf( "please input book name: ");
scanf( "%s ",&p_after-> name);
printf( "please input book author: ");
scanf( "%s ",&p_after-> author);
if(i=1)
h=p_front;
else
{
p_front-> next=p_after;
p_front=p_after;
}
p_after=(struct book *)malloc(sizeof(struct book));
if(p_after==NULL)
{
printf( "out of memory\n ");
exit(0);
}
}
p_after-> next=NULL;
print_book(h);
return h;
}
struct book *del_book(struct book *h,int id)
{
struct book *p=h,*p0;
while(id!=p-> id&&p-> next!=NULL)
{
p0=p;p=p-> next;
}
if(id==p-> id)
{
if(p==h)
{
h=p-> next;
}
else
{
p0-> next=p-> next;
free(p);
printf( "delete successful\n ");
getche();
}
}
else
{
printf( "book does not exist.\n ");
getche();
}
return h;
}
int menu()
{
int m;
system( "cls ");
printf( "sample book management system\n "/*简单图书管理系统*/
"1.add new book\n "
"2.delete books\n "
"3.show book info\n "
"4.quit\n ");
scanf( "%d ",&m);
return m;
}
int main()
{
struct book *head;
int id;
for(;;)
{
switch(menu())
{
case 1:head=add_book();break;
case 2:
{
printf( "input the book id: ");/*输入要删除的书籍的编号*/
scanf( "%d ",&id);
head=del_book(head,id);
break;
}
case 3:head=print_book(head);break;
case 4:exit(0);
}
}
return 0;
}
[解决办法]
if(i=1)
h=p_front;
改成
if(i==1)
h=p_front;
[解决办法]
PS:
while(i <=n)
改成
while(i <n)
要不然总数会多1
[解决办法]
把Add程序改一下:
struct book *add_book()
{
struct book *p_front,*p_after,*h=NULL;
int n,i=0;
printf( "how many books you want to add: ");/*输入要添加的书的数量*/
scanf( "%d ",&n);
p_after=p_front=(struct book *)malloc(sizeof(struct book));
while(i <n)
{
i=i+1;
p_after=p_front=(struct book *)malloc(sizeof(struct book));
printf( "No.%d\n ",i);
printf( "please input book id: ");
scanf( "%d ",&p_after-> id);
getchar();
printf( "please input book name: ");
scanf( "%s ",&p_after-> name);
getchar();
printf( "please input book author: ");
scanf( "%s ",&p_after-> author);
getchar();
if(i==1)
h=p_front;
else
{
p_front-> next=p_after;
p_front=p_after;
}
if(p_after==NULL)
{
printf( "out of memory\n ");
exit(0);
}
}
p_after-> next=NULL;
print_book(h);
return h;
}
把malloc放在while里面,去掉地下的那个malloc。你本来的程序最后分配的没有赋值,所以在输出时会有错误。相当与你多分配了一次空间而没有用。
[解决办法]
ls的兄弟,按照你的写法,还是不行,我什么也书没加,直接调用print_book函数,它运行的是else部分的语句,并提示程序运行错误,关闭或者调试....怎么回事?谢谢
----------------------------------
int main()
{
struct book *head=NULL; //应该对它赋初值.不然它可能有个随机值,下面没法判断.
[解决办法]
int main()
{
struct book *head=NULL;//这里错误,定义指针时一定要给它一个值,不然会引起严重错误
int id;
for(;;)
{
switch(menu())
{
case 1:head=add_book();break;
case 2:
{
printf( "input the book id: ");/*输入要删除的书籍的编号*/
scanf( "%d ",&id);
head=del_book(head,id);
break;
}
case 3:head=print_book(head);break;
case 4:exit(0);
}
}
return 0;
}
已经解决