《一个带有头结点的单向有序线性链表插入一个元素的算法实现》由会员分享,可在线阅读,更多相关《一个带有头结点的单向有序线性链表插入一个元素的算法实现(2页珍藏版)》请在金锄头文库上搜索。
1、厂给定程序中己建立一个带有头结点的单向链表,链表中的各结点按结点数据域中的数据递 增有序链接。函数fun的功能是:把形参x的值放入一个新结点并插入到链表中,插入后各 结点数据域的值仍保持递增有序。*/# include# include# defi ne typedefdata;N 8 struct listint struct list 床next; SLIST;void fun( SLIST *h, int x) SLIST *P/ *q, *s;s=(SLIST *)malloc(sizeof(SLIST); s-data=x;q=h;p=h-n ext;while(p!=NULL &
2、xp-data) q二P; p=p-n ext;son ext=p; q-next=s;SLIST *creatlist(int *a)SLIST *h/p/q; int i; h=p=(SLIST *)malloc(sizeof(SLIST); for(i=0; idata=ai; pn ext=q; p=q;p-next=0;return h;void outlist(SLIST *h)SLIST *p;p=h-n ext;if (p=NULL) printf(nThe list is NULL!n); else printf(nHead);doprintf(,-%d,p-data);p=p-next; while(p!=NULL);printf(,-Endn,);main()SLIST *head;int x;int aN=ll,12,15,18,19,22,25,29; head=creatlist(a);printf(nThe list before inserting:n); outlist(head); printf(nnEnter a number:);scanf(,l%d,/&x);fun (head,x);printf(nnThe list after inserting:nH); outlist(head);