欢迎来到天天文库
浏览记录
ID:57647147
大小:37.34 KB
页数:7页
时间:2020-08-30
《双向链表基本操作.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、双向链表输入一个双向链表,显示些双向链表并对此双向链表排序运行结果:源程序:#include#include#includetypedefstructLink/*双向链表结构体*/{intdata;structLink*lift;structLink*right;}linkx,*linky;linkyInit();/*建立双向链表*/voidPrLink(linkyp);/*输出双向链表*/linkySort(linkyhead);/*对双向链表
2、排序*/linkySwap(linkyhead,linkyone,linkytwo);/*任意交换双向链表两个结点的地址*/voidmain(void){linkyhead;head=Init();head=Sort(head);PrLink(head);}linky(Init())/*建立链表*/{linkyp,q,head;intn=0;head=p=q=(linky)malloc(sizeof(linkx));printf("排序前的链表:");scanf("%d",&p->data);/*输入数据*
3、/head->lift=NULL;n++;while(n!=10)/*一直输入到规定的数字个数停止*/{q=p;p=(linky)malloc(sizeof(linkx));scanf("%d",&p->data);/*输入数据*/q->right=p;p->lift=q;n++;}p->right=NULL;return(head);}linkySwap(linkyhead,linkyone,linkytwo)/*任意交换两个结点*/{linkytemp;if(one->lift==NULL&&two->
4、right==NULL)/*首和尾巴的交换*/{if(one->right==two)/*只有两个结点的情况下*/{two->right=one;two->lift=NULL;one->lift=two;one->right=NULL;head=two;}else/*有间隔的首尾交换*/{one->right->lift=two;two->lift->right=one;two->right=one->right;one->lift=two->lift;two->lift=one->right=NULL;h
5、ead=two;/*尾结点成为头结点*/}}elseif(two->right==NULL)/*尾和任意一个交换*/{if(one->right==two)/*交换最后两个结点*/{one->lift->right=two;two->lift=one->lift;two->right=one;one->lift=two;one->right=NULL;}else/*和前面其他结点交换*/{temp=two->lift;temp->right=one;one->lift->right=two;one->rig
6、ht->lift=two;two->lift=one->lift;two->right=one->right;one->lift=temp;one->right=NULL;}}elseif(one->lift==NULL)/*头和任意一个交换*/{if(one->right==two)/*交换头两个结点*/{two->right->lift=one;one->right=two->right;one->lift=two;two->right=one;two->lift=NULL;head=two;}else
7、/*头结点和后面其他结点交换*/{temp=one->right;temp->lift=two;one->lift=two->lift;one->right=two->right;two->lift->right=one;two->right->lift=one;two->right=temp;two->lift=NULL;head=two;/*交换的结点成为头结点*/}}else/*当中的任意两个交换*/{if(one->right==two)/*交换连在一起的两个结点*/{temp=one->lift;
8、one->lift->right=two;one->right->lift=two;one->lift=two;one->right=two->right;two->right->lift=one;two->right=one;two->lift=temp;}else/*交换隔开的两个结点*/{one->lift->right=two;one->right->lift=two;one->lift=two->lif
此文档下载收益归作者所有