欢迎来到天天文库
浏览记录
ID:11138278
大小:37.50 KB
页数:4页
时间:2018-07-10
《c_lab8-5_循环单链表和双向链表_answer _fixed》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、实验8-5循环单链表和双向链表【实验目的】1.掌握带有头结点的单循环链表的基本操作2.掌握带有头结点的双链表的基本操作【实验内容】Part1:基础练习(课堂练习)1.以下函数实现尾插法建立由几个结点组成的环形链表(带头结点),表中每个结点包含整型数据域num和指针域link,第i个结点的数据域值为i。函数返回环形链表的头指针head。请填空完成函数设计。typedefintdatatype;typedefstructnode{datatypedata;structnode*next;}linklist;linklis
2、t*Initial(intm){inti;linklist*head,*p,*s;head=(linklist*)malloc(sizeof(linklist));p=head;for(i=1;i<=m;i++){s=(linklist*)malloc(sizeof(linklist));s->data=i;p->next=s;p=s;}p->next=head;returnhead;}1.以下min函数的功能是:计算循环单链表first中每三个相邻结点数据域中的和,并返回其中最小值。请填空完成函数设计。typede
3、fintdatatype;typedefstructnode{datatypedata;structnode*next;}linklist;intmin(linklist*first){linklist*p;intsum,msum;p=first;msum=p->data+p->next->data+p->next->next->data;for(p=p->next;p!=first;p=p->next){sum=p->data+p->next->data+p->next->next->data;if(sum4、m)msum=sum;}returnmsum;}2.以下函数采用尾插法实现双向链表的建立。链表中每个结点包含数据域info,后继指针域next,前驱指针域pre。链表的头、尾指针分别放在数组a的两个元素中,链表结点中的数据通过键盘输入,当输入数据为-1时,表示输入结束。请填空完成程序设计。typedefintdatatype;typedefstructnode{structnode*pre;datatypeinfo;structnode*next;}linklist;voidCreate(linklist*ptr[25、]){linklist*head,*tail,*p;intdata;//创建头结点head=(linklist*)malloc(sizeof(linklist));head->pre=head->next=NULL;tail=head;scanf("%d",&data);while(data!=-1){p=(linklist*)malloc(sizeof(linklist));p->info=data;p->next=tail->next;p->pre=tail;//下一句无必要:由于尾插法,因此新结点后无后继结点,6、故无需修改其前驱指针//tail->next->pre=p;tail->next=p;tail=p;scanf("%d",&data);}ptr[0]=head;ptr[1]=tail;}1.以下函数实现从带有头结点的双向链表中删除值为num的一个结点,链表中每个结点包含整型数据域info、后继指针域next和前驱指针域pre。链表的头、尾指针分别放在数组a的两个元素中。请填空完成程序设计。typedefintdatatype;typedefstructnode{structnode*pre;datatypeinfo7、;structnode*next;}linklist;intDel(linklist*ptr[2],intnum){linklist*head=ptr[0],*tail=ptr[1],*p;if(head->next==NULL){printf("listisnull!");return0;}else{p=head->next;while(p!=NULL&&p->info!=num)p=p->next;if(p->info==num){if(p==tail){tail=p->pre;tail->next=NULL8、;}else{p->next->pre=p->pre;p->pre->next=p->next;}free(p);}else{printf("node%disnotexist!",num);return0;}}ptr[0]=head;ptr[1]=tail;return1;}Part2:巩固提高(课后实践)1.Part1中习题1采用尾插法创
4、m)msum=sum;}returnmsum;}2.以下函数采用尾插法实现双向链表的建立。链表中每个结点包含数据域info,后继指针域next,前驱指针域pre。链表的头、尾指针分别放在数组a的两个元素中,链表结点中的数据通过键盘输入,当输入数据为-1时,表示输入结束。请填空完成程序设计。typedefintdatatype;typedefstructnode{structnode*pre;datatypeinfo;structnode*next;}linklist;voidCreate(linklist*ptr[2
5、]){linklist*head,*tail,*p;intdata;//创建头结点head=(linklist*)malloc(sizeof(linklist));head->pre=head->next=NULL;tail=head;scanf("%d",&data);while(data!=-1){p=(linklist*)malloc(sizeof(linklist));p->info=data;p->next=tail->next;p->pre=tail;//下一句无必要:由于尾插法,因此新结点后无后继结点,
6、故无需修改其前驱指针//tail->next->pre=p;tail->next=p;tail=p;scanf("%d",&data);}ptr[0]=head;ptr[1]=tail;}1.以下函数实现从带有头结点的双向链表中删除值为num的一个结点,链表中每个结点包含整型数据域info、后继指针域next和前驱指针域pre。链表的头、尾指针分别放在数组a的两个元素中。请填空完成程序设计。typedefintdatatype;typedefstructnode{structnode*pre;datatypeinfo
7、;structnode*next;}linklist;intDel(linklist*ptr[2],intnum){linklist*head=ptr[0],*tail=ptr[1],*p;if(head->next==NULL){printf("listisnull!");return0;}else{p=head->next;while(p!=NULL&&p->info!=num)p=p->next;if(p->info==num){if(p==tail){tail=p->pre;tail->next=NULL
8、;}else{p->next->pre=p->pre;p->pre->next=p->next;}free(p);}else{printf("node%disnotexist!",num);return0;}}ptr[0]=head;ptr[1]=tail;return1;}Part2:巩固提高(课后实践)1.Part1中习题1采用尾插法创
此文档下载收益归作者所有