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