为什么插入结点时候造成死循环啊解决思路
为什么插入结点时候造成死循环啊?
我自己写的一个简单图书管理系统,课程设计作业.
可是问题出在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
{
我自己写的一个简单图书管理系统,课程设计作业.
可是问题出在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
{