




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、一 实验目的:1)掌握双向线性链表的逻辑特征2)熟练掌握带头结点的双向链表的指针操作,能完成双向链表的构建,插入,删除与显示等复杂应用。二实验原理双向链表的结点中有两个指针域,其一指向直接后继,另一指向直接前驱,在C语言中可描述如下:typedef struct DuLNodeElemType data;struct DuLNode *prior;struct DuLNode *next;DuLNode,*DuLinkList;双向链表的操作和单链表类似。本实验利用函数以及指针处理双向链表的知识,首先建立四个函数,creat,print,ListInsert_DuL,ListDelete_Du
2、L分别实现对双向链表的建立,输出,插入,删除功能。还需建立GetElemP_DuL函数来辅助。然后将五个函数组织在一个C程序中,用main函数作主调函数。三实验内容1首先建立一个带头结点的非空的双向链表。建立函数creat,操作与建立线性链表类似,但需要修改两个方向的指针。本实验以输入6个学生的数据为例。2. 建立函数GetElemP_DuL,得到第i个元素的位置指针。3对链表进行插入操作。建立函数ListInsert_DuL,输入要插入的学生数据以及位置,调用函数GetElemP_DuL,插入成功返回1,否则返回0。4. 对链表进行删除操作。建立函数ListDelete_DuL,输入要删除的
3、学生位置,调用函数GetElemP_DuL,删除成功返回1,否则返回0。5. 对链表的结果进行输出与显示。建立函数print,对链表进行正向和反向的输出。6. 建立main函数,把以上五个函数整合到一个程序之中,当输入的数据不为0时可进行多次删除,插入操作,并对每一次的结果进行显示。四实验方法运行环境:Visual C+6.0把所有程序思想写成代码,通过Visual C+编译,得到结果,即运行成功。本实验程序代码:#includestdio.h#includemalloc.h#define NULL 0#define LEN sizeof(struct student)#define OK 1
4、#define ERROR 0struct studentint data;struct student *prior;struct student *next;int n;struct student *creat(void)struct student *L;struct student *p1,*p2;L=(struct student *)malloc(LEN);L-next=NULL;L-prior=NULL;n=0;p1=p2=(struct student *)malloc(LEN);scanf(%d,&p1-data);while(p1-data!=0)n=n+1;if(n=1
5、)L-next=p1;p1-prior=L;elsep2-next=p1;p2-next-prior=p2;p2=p1;p1=(struct student *)malloc(LEN);scanf(%d,&p1-data);p2-next=L;L-prior=p2;return (L-next);void print(struct student *head)struct student *p;p=head;printf(正向输出:n);if(head!=NULL)doprintf(%dn,p-data);p=p-next;while(p!=head-prior);printf(反向输出:n)
6、;if(p=head-prior)p=p-prior;doprintf(%dn,p-data);p=p-prior;while(p!=head-prior);struct student *GetElemP_DuL(struct student *L,int i)struct student *p;int j=1;p=L;while(j!=i)p=p-next;j+;return p;int ListInsert_DuL(struct student *L,int i,int e)struct student *p,*s; if(!(p=GetElemP_DuL(L,i)return ERRO
7、R;if(!(s=(struct student *)malloc(LEN)return ERROR;s-data=e;s-prior=p-prior;p-prior-next=s;s-next=p;p-prior=s;n=n+1;return OK;int ListDelete_DuL(struct student *L,int i)struct student *p;int e;if(!(p=GetElemP_DuL(L,i)return ERROR;e=p-data;p-prior-next=p-next;p-next-prior=p-prior;free(p);n=n-1;return OK;void main()struct student *head;int i,e;printf(请输入学生的数据:n);head=creat();print(head);printf(请输入要插入的位置以及数据:n);scanf(%d%d,&i,&e);while(e!=0)if(ListInsert_DuL(head,i,e)=1)print(head);printf(请输入要插入的位置以及数据:n);scanf(%d%d,&i,&e);printf(请输入要删除的位置:n);scanf(%d,&i);while(i!=0)if(List
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论