《C语言创建单链表》由会员分享,可在线阅读,更多相关《C语言创建单链表(2页珍藏版)》请在金锄头文库上搜索。
1、#include#include#includestruct nodeint data;struct node *next;struct node *creat()/创建链表struct node *p,*q,*head;int i=1,j;p=head=(struct node *)malloc(sizeof(struct node);printf(请输入第%d个数字,i);scanf(%d,&p-data);head=p;while(p-next!=NULL&p-data!=0)q=(struct node *)malloc(sizeof(struct node);i+;printf(请输
2、入第%d个数字,i);scanf(%d,&q-data);j=q-data;p-next=q;p=q;if(j=0)free(p);p-next=NULL;p=head;while(p!=NULL)printf(%dn,p-data);p=p-next;return(head);int main() creat();printf(创建链表n);return 0;void destroy()struct node *r,*s;p=head-next;int i=1; if(p!=NULL) printf(链表为空或不存在n); return ; while(s!=NULL) printf(the %d date is %dn,i,s-date);i=i+1;s=s-next; while(p!=NULL) r=p-next ;free(p);p=r; void main()int a; void creat(int a); void destroy(); printf(输入链表长度_); scanf(%d,&a); creat(a); printf(销毁链表n); / destroy();