ImageVerifierCode 换一换
格式:DOCX , 页数:14 ,大小:92.31KB ,
资源ID:2776194      下载积分:12 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/2776194.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(单链表实验报告.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

单链表实验报告.docx

1、单链表实验报告单链表练习实验实验目的:熟练掌握单的基本操作及简单应用。实验内容:实验题目 1 .书上习题2.6中的第5题。(单链表的逆置) 2.书上习题2.6中第6题。 (删除已知值k的结点)程序代码#include #include #include typedef int ELEMTYPE;struct node ELEMTYPE element;struct node *next; ;struct node *head; struct node *tail; void init() head=(struct node *)malloc(sizeof(struct node); if (!

2、head) exit(0); head-next=NULL; tail=head; int isEmpty() if (head=tail) return 1; else return 0;int length() struct node *p; int cnt=0; for (p=head-next;p!=NULL;p=p-next) cnt+; return cnt; void append(ELEMTYPE item)struct node *p; p=(struct node *)malloc(sizeof(struct node); if (!p) exit(0); p-elemen

3、t=item; p-next=tail-next; tail-next=p; tail=p;struct node *search(ELEMTYPE item) struct node *p; p=head-next; while(p!=NULL) if (p-element=item) return p; else p=p-next; return NULL; void insert_x(ELEMTYPE x)/将元素x按序插入到单链表 函数 struct node *p,*q,*item;item=(struct node *)malloc(sizeof(struct node);/创建结

4、点 if(!item) exit(0);item-element=x;p=head-next;while(p)/从第一个结点开始比较与x的大小 if(p-elementitem-element)q=head;while(q-next!=p)/求前驱 q=q-next;item-next=p;/实现插入操作 q-next=item;break;else p=p-next;if(!p)/链表中元素都比较完毕后,实现将x插入到链表尾 item-next=tail-next;tail-next=item;tail=item;void traversal() struct node *p; printf

5、 (list is :);p=head-next; for(p=head-next;p!=NULL;p=p-next) printf (%d,p-element); printf (n);void invert() /逆置函数,借用老师课堂上的“头变尾,尾变头,中间结点指针方向逆置”struct node *p,*q,*k; /的思想。先定义三个指针,分别依次指向第一第二第三个结点(头p=head-next ;q=p-next ;k=q-next ;/结点除外)处于第二位的指针指向第一位,然后三个while(k) /指针逐次后移,直到k指针指向NULL;q-next =p;p=q; /左边wh

6、ile循环就是该算法的实现代码q=k;k=q-next;q-next=p; /实现完循环后,再进行最后结点的逆置head-next-next=NULL; /将原来的第一个结点变为尾结点head-next =q; /将头结点与原来的尾结点连接int main()init();append(1);append(2);append(3);append(5);append(6);append(7);printf(依次输出原有元素n);traversal();insert_x(4);printf(依次输出插入x后所有元素n);traversal();invert();printf(输出逆置后的元素n);

7、traversal(); 关键部分加注释(见源代码注释)执行结果 算法分析void invert() 函数q-next =p;p=q; q=k;k=q-next;上述四个语句为循环结构中的基本语句,设共有n个结点(头结点除外),则循环执行次数为n2,时间复杂度为o(n); void insert_x(ELEMTYPE x)函数while(p)if(p-elementitem-element)q=head;while(q-next!=p)q=q-next;上述即为循环结构语句,设共有n个结点若比x值大的结点出现在第i个结点,则内循环需进行i1次,则平均次数 n1/n*(i1)=(n1)/2 则f

8、(n)=c1(n1)*n/2+c2n +c3(c为常数);时间复杂度为o(n2); i=1 3.用单链表实现两个集合的合并。#include #include #include typedef int ELEMTYPE;struct node ELEMTYPE element;struct node *next; ;void init(struct node *&head, struct node *&tail) head=(struct node *)malloc(sizeof(struct node); if (!head) exit(0); head-next=NULL; tail=he

9、ad; int isEmpty(struct node *&head, struct node *&tail) if (head=tail) return 1; else return 0;int length(struct node *&head, struct node *&tail) struct node *p; int cnt=0; for (p=head-next;p!=NULL;p=p-next) cnt+; return cnt; void append(struct node *&head,struct node *&tail,ELEMTYPE item)struct nod

10、e *p; p=(struct node *)malloc(sizeof(struct node); if (!p) exit(0); p-element=item; p-next=tail-next; tail-next=p; tail=p;struct node *get(struct node *&head,struct node *&tail,int i) struct node *p; int j; p=head-next; for(j=1;(p!=NULL)&(jnext; if(!p)|(ji) return NULL; return p; void traversal(stru

11、ct node *&head, struct node *&tail) struct node *p; printf (list is :);p=head-next; for(p=head-next;p!=NULL;p=p-next) printf (%d,p-element); printf (n); int main() /实现单链表局部化struct node *head1;struct node *tail1;/定义三个头和尾指针struct node *head2;struct node *tail2;struct node *head3;struct node *tail3;hea

12、d1=(struct node *)malloc(sizeof(struct node);/分别给六个指针分配六个指向的内存空间tail1=(struct node *)malloc(sizeof(struct node);/防止野指针出现head2=(struct node *)malloc(sizeof(struct node);tail2=(struct node *)malloc(sizeof(struct node);head3=(struct node *)malloc(sizeof(struct node);tail3=(struct node *)malloc(sizeof(s

13、truct node);init(head1,tail1); /初始化三个链表init(head2,tail2);init(head3,tail3);append(head1,tail1,1);append(head1,tail1,2);append(head1,tail1,3);append(head2,tail2,4);append(head2,tail2,4);append(head2,tail2,3);append(head2,tail2,2);printf(输出第一个链表的元素n);traversal(head1,tail1);printf(输出第二个链表的元素n);traversal(head2,tail2);int i; /利用循环实现将第一个链表追加到第三个链表后for(i=1;ielement);/利用get函数访问第一个链表中元素for(i=1;ielement);printf(输出合并后的链表元素n); traversal(head3,tail3);关键部分加注释(见源代码注释)执行结果 算法分析for(i=1;i=length(head1,tail1);i+)append(

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1