level 1
七月的qiyue
楼主
用VC编的程序,图书馆管理系统,里面有一个子程序要删除书籍信息,按照图书的编号删除建立的结构体为struct book/*书籍信息*/
{int number;int condition;/*书籍状态:在馆为1,不在为0*/
char name[200];/*书名*/
char author[100]; /*作者*/
char publish[200];/*出版社*/
long year; /*出版年份*/
char ISBN[30];/*ISBN码*/
float price;/*书籍价格*/
int n; /*借出次数*/
char user[30];
struct book *next;
};
typedef struct book BOOK;
删除的子程序为
/*删除书籍*/
BOOK *DeleteBook(BOOK *head)
{int i=0;
int nodeNum;
char c;
do{
printf("请输入要删除的图书编号:\n");
scanf("%d",&nodeNum);
head=DeleteNode(head,nodeNum);
printf("是否继续?(y/n):");
scanf(" %c",&c);
}while(c=='y'||c=='Y');
return head;
}
BOOK *DeleteNode(BOOK *head, int nodeNum)
{
BOOK *p=head,*pr=head;
if(head==NULL)
{
printf("No linked table!");
return (head);
}
while (head->number==nodeNum&&head->next!=NULL)
{
head=head->next;
free(p);
free(pr);
p=pr=NULL;
return head;
}
while (nodeNum!=p->number&&p->next!=NULL)
{ pr=p;
p=p->next;
}
if (nodeNum==p->number)
{
if(p==head)
{
head=p->next;
}
else
{
pr->next=p->next;
}
free(p);
}
else
{
printf("未找到该书籍!");
}
return head;
}
主要就是删除第一个节点的问题,后面的节点都没问题,删除时只要输入的是删除第一本程序就直接死掉。如果把free(p);free(pr);删掉的话可以正常运行,但删不掉第一个节点。求大神指点,在线等解答
2011年06月14日 11点06分
1
{int number;int condition;/*书籍状态:在馆为1,不在为0*/
char name[200];/*书名*/
char author[100]; /*作者*/
char publish[200];/*出版社*/
long year; /*出版年份*/
char ISBN[30];/*ISBN码*/
float price;/*书籍价格*/
int n; /*借出次数*/
char user[30];
struct book *next;
};
typedef struct book BOOK;
删除的子程序为
/*删除书籍*/
BOOK *DeleteBook(BOOK *head)
{int i=0;
int nodeNum;
char c;
do{
printf("请输入要删除的图书编号:\n");
scanf("%d",&nodeNum);
head=DeleteNode(head,nodeNum);
printf("是否继续?(y/n):");
scanf(" %c",&c);
}while(c=='y'||c=='Y');
return head;
}
BOOK *DeleteNode(BOOK *head, int nodeNum)
{
BOOK *p=head,*pr=head;
if(head==NULL)
{
printf("No linked table!");
return (head);
}
while (head->number==nodeNum&&head->next!=NULL)
{
head=head->next;
free(p);
free(pr);
p=pr=NULL;
return head;
}
while (nodeNum!=p->number&&p->next!=NULL)
{ pr=p;
p=p->next;
}
if (nodeNum==p->number)
{
if(p==head)
{
head=p->next;
}
else
{
pr->next=p->next;
}
free(p);
}
else
{
printf("未找到该书籍!");
}
return head;
}
主要就是删除第一个节点的问题,后面的节点都没问题,删除时只要输入的是删除第一本程序就直接死掉。如果把free(p);free(pr);删掉的话可以正常运行,但删不掉第一个节点。求大神指点,在线等解答