数据结构实验1.docx
- 文档编号:10550475
- 上传时间:2023-05-26
- 格式:DOCX
- 页数:19
- 大小:255.68KB
数据结构实验1.docx
《数据结构实验1.docx》由会员分享,可在线阅读,更多相关《数据结构实验1.docx(19页珍藏版)》请在冰点文库上搜索。
数据结构实验1
数据结构《实验1》实验报告
实验项目1:
线性表存储及运算
学 号
1209030109
姓 名
陶爽
课程号
实验地点
指导教师
时间
评语:
按时完成实验;实验内容和过程记录完整;回答问题完整、正确;实验报告的撰写认真、格式符合要求;无抄袭的行为。
成绩
教师签字
线性表链式存储(双向链表)插入、删除运算
1、预习要求:
线性表的插入、删除相关概念及运算,完成线性表元素的插入、删除。
2、实验目的:
(1)了解线性表的插入、删除相关概念;
(2)理解线性表的插入、删除过程和结构定义;
(3)掌握算法转换为程序的过程中的变化。
3、实验内容及要求:
(1)分别建立包含10个数据元素的链式存储线性表;
(2)从键盘输入一个数据元素,插入到线性表中第k(包含0号位置)个位置;
(3)从键盘输入一个数据元素关键字或位置k(包含1号位置),从线性表中删除相应数据元素;
(4)给出程序及插入、删除前和插入、删除后线性表结果。
4、实验设备(环境)及要求
硬件:
支持IntelPentiumⅡ及其以上CPU,内存128MB以上、硬盘1GB以上容量的微机。
软件:
配有Windows98/2000/XP操作系统,安装VisualC++。
5、实验时间:
6学时
6、该文档的文件名不要修改,存入<学号><姓名>命名的文件夹中
7、该表中的数据只需填空,已有内容不要修改
实验结果(运行结果界面及源程序,运行结果界面放在前面):
截图如下:
程序如下:
#definePersonEType
#defineHeadETypeint
#include
#include
#include
#include
structPerson
{
charname[7];
charplace[7];
charsex[3];
charnumber[6];
};
structDoubleNode
{
ETypedata;
DoubleNode*nlink;
DoubleNode*plink;
};
structHeadNode
{
HeadETypeHdata;
DoubleNode*first;
};
typedefHeadNode*DoubleChainList;
voidCreatDoubleChainList(DoubleChainList&l)
{//创建双向链表
l=newHeadNode;
l->first=NULL;
}
voidOutputDoubleChainList(DoubleChainList&l)
{//输出双向链表内容
cout<<"姓名"< DoubleNode*current=l->first; cout<<""; while(current) { cout< current=current->nlink; } cout< cout<<"性别"< current=l->first; cout<<""; while(current) { cout< current=current->nlink; } cout< cout<<"地点"< current=l->first; cout<<""; while(current) { cout< current=current->nlink; } cout< cout<<"序号"< current=l->first; cout<<""; while(current) { cout< current=current->nlink; } cout< } boolGetDoubleChainList(DoubleChainList&l,intk,EType&result) {//取出第K个元素 if(k<1) returnfalse; intindex=1; DoubleNode*current=l->first; while(index { current=current->nlink; index++; } if(current) { strcpy(result.name,current->data.name); strcpy(result.sex,current->data.sex); strcpy(result.number,current->data.number); strcpy(result.place,current->data.place); returntrue; } returnfalse; } boolDeleteDoubleChainList(DoubleChainList&l,intk) {//删除某个元素 if(k<1||! l->first) returnfalse; DoubleNode*current=l->first; DoubleNode*p; if(k==1) { p=current->nlink; if(p) p->plink=NULL; l->first=p; } else { DoubleNode*q=l->first; for(intindex=1;index { q=q->nlink; } if(! q) returnfalse; current=q; q=current->plink; p=current->nlink; q->nlink=p; if(p) { p->plink=q; } } deletecurrent; returntrue; } boolInsertDoubleChainList(DoubleChainList&l,intk,EType&newy) {//插入 if(k<0) returnfalse; intindex=1; DoubleNode*current=l->first; while(index { index++; current=current->nlink; } if(k>0&&! current) returnfalse; DoubleNode*q=newDoubleNode; strcpy(q->data.name,"韩婷"); strcpy(q->data.sex,"女"); strcpy(q->data.number,"QQQ1"); strcpy(q->data.place,"北京"); if(k) { q->nlink=current->nlink; q->plink=current; DoubleNode*p=current->nlink; if(p) { p->plink=q; } current->nlink=q; } else { q->nlink=l->first; q->plink=NULL; DoubleNode*p=l->first; if(p) p->plink=q; l->first=q; } returntrue; } intLengthDoubleChainList(DoubleChainList&l) {//计算该链表的长度 DoubleNode*current=l->first; intlen=0; while(current) { len++; current=current->nlink; } returnlen; } DoubleNode*SearchDoubleChainList(DoubleChainList&l,EType&newx) {//查找关键字,并返回地址 DoubleNode*current=l->first; while(current&&(strcmp(current->data.name,newx.name)||strcmp(current->data.sex,newx.sex)||strcmp(current->data.place,newx.place)||strcmp(current->data.number,newx.number))) { current=current->nlink; } if(current) returncurrent; returnNULL; } intmain() { DoubleChainListl; intx,k; charkey; DoubleNode*p,*q; ETyperesult,newy,newx; CreatDoubleChainList(l); charname[][7]={"梁非","顾烟","容岩","叶沐","秦宋","小离","纪南","李岩","秦桑","小白"}; charplace[][7]={"北京","北京","北京","北京","北京","北京","北京","北京","北京","北京"}; charsex[][3]={"男","女","男","女","男","女","女","男","女","男"}; charnumber[][5]={"AAA1","AAA2","BBB1","BBB2","CCC1","CCC2","DDD1","DDD2","EEE1","EEE2"}; for(inti=1;i<=10;i++) { p=newDoubleNode; strcpy(p->data.name,name[10-i]); strcpy(p->data.sex,sex[10-i]); strcpy(p->data.place,place[10-i]); strcpy(p->data.number,number[10-i]); p->nlink=l->first; q=l->first; if(! l->first) { p->nlink=NULL; p->plink=NULL; l->first=p; } else { p->nlink=l->first; p->plink=NULL; l->first->plink=p; l->first=p; } } while(true) { cout< cout<<""; cout<<"'盛开'系列人物关系双向链表"< system("pause"); system("cls"); cout<<"长着翅膀的大灰狼盛开系列小说: "< cout<<"********************************************************"< cout<<"********************1-输出该双向链表的内容**************"< cout<<"********************2-取该链表中第K个元素***************"< cout<<"********************3-删除该链表中第K个元素*************"< cout<<"********************4-在第K个元素后面插入新元素*********"< cout<<"********************5-查找元素C*************************"< cout<<"********************6-输出该链表的长度******************"< cout<<"********************0-退出******************************"< cout<<"********************************************************"< cout<<"****请输入需要执行的功能序号X***"< cin>>x; switch(x) { case1: { system("cls"); cout<<"盛开人物链表内容如下: "< cout< OutputDoubleChainList(l); break; } case2: { system("cls"); cout<<"取该链表中的第K个元素"< cout<<"请输入需要取出元素编号K"< cin>>k; if(GetDoubleChainList(l,k,result)) { cout<<"查找成功! ! ! "< cout< } else { cout<<"查找不成功! ! ! "; cout< } break; } case3: { system("cls"); cout<<"删除第K个元素"< cout<<"请输入要删除元素的编号K: "; cin>>k; cout< if(DeleteDoubleChainList(l,k)) { cout<<"删除成功! ! ! "< cout<<"删除后的链表内容如下: "< OutputDoubleChainList(l); cout< } else { cout<<"删除不成功! ! ! "< } break; } case4: { system("cls"); cout<<"插入新元素C"< cout<<"请输入要插入到第K个元素后面的K值: "; cin>>k; cout< if(InsertDoubleChainList(l,k,newy)) { cout<<"插入成功! ! ! ! "< OutputDoubleChainList(l); cout< } else { cout<<"插入不成功! ! ! ! ! "; cout< } break; } case5: { system("cls"); cout<<"查找关键字! ! ! "< strcpy(newx.name,"梁非"); strcpy(newx.place,"北京"); strcpy(newx.number,"AAA1"); strcpy(newx.sex,"男"); cout<<"********返回该元素的地址: *****************"< cout< break; } case6: { system("cls"); cout<<"输出该链表的长度L: "< cout< break; } case0: { system("cls"); return0; break; } } system("pause"); system("cls"); } return0; }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 实验