#include<stdio.h> #include<time.h> #define ElemType int struct node { ElemType data; struct node * next; }; typedef struct node NODE; NODE * CreateLinkList(int n) { int i; NODE *p,*q; ElemType a; q=(NODE *)malloc(sizeof(NODE)); a=rand()%20; q->data = a; q->next=NULL; for(i=n-1;i>=1;i--) { p=(NODE *)malloc(sizeof(NODE)); a=rand()%20; p->data=a; p->next=q; q=p; } return q; } NODE * InsertLinkList(NODE * head,ElemType x,ElemType y) { NODE *s,*p,*q; s=(NODE *)malloc(sizeof(NODE)); s->data=y; s->next=NULL; for(p=head;p!=NULL;p=p->next) printf("%d\n",p->data); printf("\n"); if(head==NULL) head=s; else if (head->data == x) { s->next = head; head = s; } else { q=head; p=head->next; while(p!=NULL&&p->data!=x)
{ q=p; p=p->next; } if(p->data==x) { s->next=p; q->next = s; } else { q->next=s; } } return head; } void main() { ElemType a; NODE * head,*p; srand((unsigned)time(NULL)); head=CreateLinkList(10); for(p=head;p!=NULL;p=p->next) printf("%d\n",p->data); printf("%10d\n",a=rand()%20); head=InsertLinkList(head,a,99); for(p=head;p!=NULL;p=p->next) printf("%d\n",p->data); }
程序初步调试,发现InsertLinkList函数的while循环里出错,但不知道具体哪里错误。求牛人解决
泡面大减价
相关分类