链表死循环问题
程序代码:
#include "stdio.h" #include "conio.h" #include "stdlib.h" struct xue { int num; char name[20]; float score; struct xue *next; }; //建立链表 struct xue * LinkList() { struct xue *h,*a,*b; int i=0; printf("请输入学号,姓名,成绩\n"); if((a=b=(struct xue*)malloc(sizeof(struct xue)))==NULL) printf("内存不足"); h=NULL; scanf("%d%s%f",&a->num,a->name,&a->score); while(a->num!=0) { i++; if(i==1) h=a; else b->next=a; b=a; if((a=(struct xue*)malloc(sizeof(struct xue)))==NULL) printf("内存不足"); scanf("%d%s%f",&a->num,a->name,&a->score); } b->next=NULL; return h; } //输出链表 int print(struct xue *h1) { struct xue *k; k=h1; if(k!=NULL) { printf("学号 名字 成绩\n"); while(k!=NULL) { printf("%d\t%s\t%.2f\n",k->num,k->name,k->score); k=k->next; } } printf("\n"); return 0; } //删除链表 struct xue * Del_LinkList(struct xue * s, int n) { struct xue *i,*j; i=s; if(i==NULL) { printf("错误!该表是空的\n"); return s; } while(n!=i->num&&i!=NULL); { j=i; i=i->next; } if(i->num==n) { if(s==i) { free (s); return i->next; } else j->next=i->next; } else printf("找不到该结点"); return s; } int main() { struct xue *head; int n; head=LinkList(); print(head); printf("请输入你要删除的学号:"); scanf("%d",&n); Del_LinkList(head,n);//输入学号后...CPU使用就100%了.可能这个函数有死循环..找了好久也没有找到问题 print(head); getch(); return 0; }
[[it] 本帖最后由 bianfeng 于 2008-4-21 22:33 编辑 [/it]]