单链表实验.pdf
《单链表实验.pdf》由会员分享,可在线阅读,更多相关《单链表实验.pdf(7页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、实验二单链表实验一、实验目的1、掌握用 Visual C+6.0上机调试单链表的基本方法2、掌握单链表的插入、删除、查找、求表长以及有序单链表的合并算法的实现二、实现内容1、单链表基本操作的实现 问题描述 要在带头结点的单链表h中第 i 个数据元素之前插入一个数据元素x,首先需要在单链表中寻找到第 i-1 个结点并用指针p指示,然后申请一个由指针s 指示的结点空间,并置 x为其数据域值,最后修改第i-1 个结点,并使 x结点的指针指向第i 个结点,要在带头结点的单链表h中删除第 i 个结点,首先要计数寻找到第i 个结点并使指针p指向其前驱第 i-1 个结点,然后删除第i 个结点并释放被删除结点
2、空间。基本要求 用链式存储结构实现存储 实现提示 链式存储结构不是随机存储结构,即不能直接取到单链表中某个结点,而要从单链表的头结点开始一个一个地计数寻找。2、求表长以及有序单链表的合并算法的实现 问题描述 假设有两个按元素值递增次序排列的线性表,均以单链表形式存储。请编写算法将这两个单链表归并为一个按元素值递减次序排列的单链表,并计算表长。要求利用原来两个单链表的结点存放归并后的单链表。基本要求 用链式存储结构实现存储实验代码#include#include#define null 0/-定义节点-typedef struct List int data;struct List*next;L
3、ist;/-构造链表-List*InitList(int n)int i;List*head,*p,*q;head=(List*)malloc(sizeof(List);/构造头结点 if(!head)exit(0);/判断是否成功分配内存 q=head;head-data=0;/记录链表长度 head-next=null;for(i=0;inext=head-next;head-next=p;*/-/-尾插-p-next=q-next;q-next=p;q=p;head-data+;/长度自增 printf(输入数据第%d 个数据:n,head-data);scanf(%d,&(p-data
4、);return head;/-插入-void Insert(List*head,int position,int elem)int i;List*p,*q;if(positionhead-data+1)printf(插入位置非法!);exit(0);p=head;for(i=1;inext;q=(List*)malloc(sizeof(List);q-data=elem;q-next=p-next;p-next=q;head-data+;printf(成功将%d 插入到第%d 个位置!n,elem,position);/-删除-void Delete(List*head,int positi
5、on)int i;List*p,*q;if(!head)printf(链表为空!);exit(0);p=head;q=p-next;文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2
6、Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5
7、I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2
8、Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1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ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4 if(positionhead-data)printf(非法位置!);exit(0);for(i=1;inext;q=p-next;p-next=q-next;printf(成功删除第%d 个位置上的元素:%dn,positi
12、on,q-data);free(q);head-data-;/-排序(由小到大)-void sort(List*head)int temp;List*p,*q;for(p=head-next;(p-next)&(head-data1);p=p-next)for(q=p-next;q;q=q-next)if(q-datadata)temp=p-data;p-data=q-data;q-data=temp;/-有序合并-void Unite_List(List*head,List*nhead)int n=0;List*p,*q,*pre;sort(head);sort(nhead);pre=hea
13、d;p=pre-next;q=nhead-next;while(q)文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1
14、V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:C
15、M8W7O6L5I9 HQ7J9J5W1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ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:C
19、M8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4 if(!p)pre-next=q;head-data+=nhead-data;break;else if(q-datadata)/nhead表中当前元素小于head当前元素,插入其前面 nhead-next=q-next;/nhead链表头指针跳过 q后移nhead-data-;q-nex
20、t=p;/接入后继 pre-next=q;/接入前驱head-data+;pre=q;p=pre-next;q=nhead-next;else pre=p;p=pre-next;free(nhead);/-查找位置-void Search_Position(List*head,int elem)int i,position;List*p;p=head;if(!head)printf(链表为空!);exit(0);for(i=1;idata;i+)p=p-next;if(elem=p-data)position=i;printf(发现元素%d 在表中的位置:%dn,elem,position);
21、exit(0);文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 H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J9J5W1V6 ZQ8
25、H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6L5I9 HQ7J9J5W1V6 ZQ8H2Q4M6A4文档编码:CM8W7O6
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 单链表 实验
限制150内