数据结构《实验1》实验报告实验项目1:线性表存储及运算学号姓名课程号实验地点指导教师时间评语:按时完成实验;实验内容和过程记录完整;回答问题完整、正确;实验报告的撰写认真、格式符合要求;无抄袭的行为。
成绩教师签字线性表链式存储(双向链表)插入、删除运算1、预习要求:线性表的插入、删除相关概念及运算,完成线性表元素的插入、删除。
2、实验目的:(1)了解线性表的插入、删除相关概念;(2)理解线性表的插入、删除过程和结构定义;(3)掌握算法转换为程序的过程中的变化。
3、实验内容及要求:(1)分别建立包含10个数据元素的链式存储线性表;(2)从键盘输入一个数据元素,插入到线性表中第k(包含0号位置)个位置;(3)从键盘输入一个数据元素关键字或位置k(包含1号位置),从线性表中删除相应数据元素;(4)给出程序及插入、删除前和插入、删除后线性表结果。
4、实验设备(环境)及要求硬件:支持 Intel Pentium Ⅱ及其以上 CPU ,内存 128MB 以上、硬盘 1GB 以上容量的微机。
软件:配有 Windows98/2000/XP 操作系统,安装 Visual C++ 。
5、实验时间:6学时6、该文档的文件名不要修改,存入<学号> <姓名> 命名的文件夹中7、该表中的数据只需填空,已有内容不要修改实验结果(运行结果界面及源程序,运行结果界面放在前面):实验程序如下:#define EXPRESS EType#define HeadEType int#include<iostream>#include<string>#include<stdlib.h>using namespace std;struct EXPRESS{char name[5];char number[5];char place[5];int weight;};struct DoubleNode{EType data;DoubleNode *plink;DoubleNode *nlink;};//节点类型定义struct HeadNode{HeadEType Hdata;DoubleNode *first;} ;typedef HeadNode *DoubleChainList; //表头节点结构类型定义void CreatDoubleChainList(DoubleChainList &L){L=new HeadNode;L->first=NULL;};//构造一个空链表void OutputDoubleChainList(DoubleChainList &L) {//输出链表中的元素DoubleNode*current=L->first;while(current)current=current->nlink;current=L->first;cout<<" ";//4个空格while(current){cout<<current-><<" ";//3个空格current=current->nlink;}cout<<endl;current=L->first;cout<<" ";//8个空格while(current){cout<<current->data.number<<" ";//3个空格current=current->nlink;}cout<<endl;current=L->first;cout<<" ";//8个空格while(current){cout<<current->data.place<<" ";//3个空格current=current->nlink;}cout<<endl;current=L->first;cout<<" ";//8个空格while(current){cout<<current->data.weight<<" ";//3个空格current=current->nlink;}cout<<endl;}int LengthDoubleChainList(DoubleChainList &L){//返回链表中的节点数DoubleNode*current=L->first;int len=0;while(current){len++;current=current->nlink;}return len;}//确定链表的长度void DestroyDoubleChainList(DoubleChainList &L){DoubleNode *current;current=L->first;while(L->first){current=current->nlink;delete L->first;L->first=current;if(current)current->plink=NULL;}}//删除链表中的数据节点,并释放空间bool GetElemDoubleChainList(DoubleChainList &L,int k,EType&result){//L中第K个元素取至x中,如不存在返回false,找到返回true,result 带回if(k<1) return false;DoubleNode *current=L->first;int index=1;while(index<k && current){current=current->nlink;index++;}if(current){result=current->data;return true;}return false;//k值太大,不存在第k个节点}DoubleNode *SearchDoubleChainList(DoubleChainList &L,int &searchkey) {//查找x,如果找到则返回x的地址,如果未找到则返回NULL DoubleNode *current=L->first;while(current&&(current->data.weight!=searchkey))current=current->nlink;if(current) return current;return NULL;}bool InsertDoubleChainList(DoubleChainList &L,int &k,EType &x){//在链表中第k个元素之后中插入元素x运算,//如果不存在第k个元素或现行表已满,则返回出错状态if(k<0) return false;int index=1;DoubleNode *current=L->first;while (index<k&¤t){//找第k个节点index++;current=current->nlink;}if(k>0&&!current) return false;DoubleNode *q=new DoubleNode;q->data=x;if(k){//插入在current之后q->nlink=current->nlink;q->plink;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;}return true;}bool DeleteDoubleChainList(DoubleChainList &L,int &k){//在双向链表中删除第K个元素,如果不存在第K个元素就返回出错状态if(k<1||!L->first) return false;DoubleNode *current=L->first;DoubleNode *p;if(k==1){p=current ->nlink;if(p)p->plink=NULL;L->first=p;}else{int index=1;while(index<k-1&¤t){index++;current=current->nlink;}p=current;if(!p||!p->nlink) return false;current=current->nlink;DoubleNode *q=current->plink;if(q)q->nlink=p;p->nlink=q;}delete current;return true;}bool MoveFirstDoubleChainList(DoubleChainList &L,int k) {//在线性表L中将第k个元素移至表首if(k<1||!L->first) return false;DoubleNode *current=L->first;DoubleNode *p;if(k==1) return true;else{int index;while(index<k-1&¤t){index++;current=current->nlink;}p=current;if(!p||!p->nlink) return false;current=current->nlink;DoubleNode *q=current->nlink;if(q) q->plink=p;p->nlink=q;current->nlink=L->first;L->first=current;current->plink=NULL;return true;}}bool InvertDoubleChainList(DoubleChainList &L){//将线性表逆向DoubleNode *p=L->first;DoubleNode *current;L->first=NULL;while(p){current=p;p=p->nlink;current->nlink=L->first;current->plink=p;L->first=current;}current->plink=NULL;return true;}/* void InvertDisplayChainList(ChainNode *p){if(p){InvertDisplayChainList(p->link);cout<<p->date.weight<<"--"}}*/int main(){DoubleNode *p;DoubleChainList L;EType x,result;char name[][5]={"","小A","小B","小C","小D","小E","小F","小G","小H","小I","小J"};char number[][5]={"","11","12","13","14","15","16","17","18","19","20"};char place[][5]={"","A1","B1","C1","D1","E1","F1","G1","H1","I1","J1"};int k,searchkey,start,end,choice;//****构造链表***********CreatDoubleChainList(L);//链表初始化for(int i=1;i<11;i++){p=new DoubleNode;strcpy(p->,name[11-i]);strcpy(p->data.number,number[11-i]);strcpy(p->data.place,place[11-i]);p->data.weight=21-i; //p->nlink=L->first;p->plink=NULL;L->first=p;}while(true){cout<<endl;cout<<"************线性表简单链表存储的运算*************"<<endl;cout<<"* 1----------------输出线性表中的所有元素**"<<endl;cout<<"* 2------------------------计算链表的长度**"<<endl;cout<<"* 3-----------------在链表中查找第K个元素**"<<endl;cout<<"* 4------------在链表中查找关键字(重量)**"<<endl;cout<<"* 5-----在链表中插入新元素到第K个元素后面**"<<endl; cout<<"* 6-----------------从链表中删除第K个元素**"<<endl;cout<<"* 7------------------删除链表中的所有节点**"<<endl;cout<<"* 8----------将第K个元素移至第1个元素位置**"<<endl;cout<<"* 9------------------------将双向链表逆向**"<<endl;cout<<"* 10-------------------删除第i到第j个节点**"<<endl;cout<<"* 0----------------------------------退出**"<<endl;cout<<"*************************************************"<<endl; cout<<"请选择处理功能:";cin>>choice;cout<<endl;system("cls");switch(choice){case 1:{//1---输出线性表中的所有元素cout<<"***********输出链表中的所有元素************"<<endl;cout<<endl;OutputDoubleChainList(L);cout<<endl;break;}case 2:{//2---计算链表长度cout<<"************计算链表长度*************"<<endl<<endl;cout<<"此操作前链表状态:" <<endl<<endl;OutputDoubleChainList(L);cout<<endl;cout<<"线性表长度="<<LengthDoubleChainList(L)<<endl<<endl;break;}case 3:{//3--在链表中查找第K个元素cout<<"********在链表中查找第K个元素*********"<<endl<<endl;cout<<"此操作前链表状态:" <<endl<<endl;OutputDoubleChainList(L);cout<<endl;cout<<"输入查找第K个记录的K值:"<<endl<<endl;cin>>k;cout<<endl;if(GetElemDoubleChainList(L,k,result)){cout<<"第"<<k<<"个元素的值"<<endl<<endl;cout<<"名字:"<<<<endl;cout<<"编号:"<<result.number<<endl;cout<<"地址:"<<result.place<<endl;cout<<"重量:"<<result.weight<<endl<<endl;}elsecout<<"K值范围有误"<<endl<<endl;break;}case 4:{//4--在链表中查找符合关键字(重量)的元素cout<<"******在链表中查找符合关键字(重量)的元素*****"<<endl<<endl;cout<<"此操作前链表状态:" <<endl<<endl;OutputDoubleChainList(L);cout<<endl;cout<<"输入查找关键字weight:";cin>>searchkey;p=SearchDoubleChainList(L,searchkey);cout<<"查找结果:"<<endl<<endl;if(p){cout<<"名字:"<<p-><<endl;cout<<"编号:"<<p->data.number<<endl;cout<<"地址:"<<p->data.place<<endl;cout<<"重量:"<<p->data. weight<<endl<<endl;}elsecout<<"无此重量的包裹"<<endl<<endl;break;}case 5:{//5--在链表中插入新元素到第K个元素后面cout<<"*****在链表中插入新元素到第K个元素后面**********"<<endl<<endl;cout<<"此操作前链表状态:" <<endl<<endl;OutputDoubleChainList(L);cout<<endl;cout<<"输入插入k点的值:"; cin>>k;cout<<"输入要插入的数据点值x"<<endl<<endl;cout<<"名字:"; cin>>;cout<<"编号:"; cin>>x.number;cout<<"地址:"; cin>>x.place;cout<<"重量:"; cin>>x.weight;cout<<"插入数据x到第"<<k<<"个元素后面:"<<endl<<endl;if(InsertDoubleChainList(L,k,x))OutputDoubleChainList(L);elsecout<<"k值不正确"<<endl<<endl;break;}case 6:{//6--链表中删除第K个元素cout<<"****链表中删除第k个元素*****"<<endl<<endl;cout<<"此操作前链表状态:" <<endl<<endl;OutputDoubleChainList(L);cout<<endl;cout<<"删除第几个节点:"; cin>>k;cout<<"删除第"<<k<<"个节点:"<<endl<<endl;if(DeleteDoubleChainList(L,k))OutputDoubleChainList(L);elsecout<<"k值不正确"<<endl<<endl;break;}{//删除链表中所有节点cout<<"****删除链表中所有节点*****"<<endl<<endl;cout<<"此操作前链表状态:" <<endl<<endl;OutputDoubleChainList(L);cout<<endl;cout<<"删除整个链表后的节点为空"<<endl<<endl;DestroyDoubleChainList(L);OutputDoubleChainList(L);break;}case 8:{//将第K个节点移至第一个节点位置cout<<"******将第K个节点移至第一个节点位置****"<<endl<<endl;cout<<"此操作前链表状态:" <<endl<<endl;OutputDoubleChainList(L);cout<<endl;cout<<"将第几个节点移至第一个节点位置:"; cin>>k;cout<<"将第"<<k<<"个节点移至第一个节点位置:"<<endl<<endl;if(MoveFirstDoubleChainList(L,k))OutputDoubleChainList(L);elsecout<<"k值不正确"<<endl<<endl;break;}case 9:{//将链表逆向cout<<"********将链表逆向********"<<endl<<endl;cout<<"此操作前链表状态:" <<endl<<endl;OutputDoubleChainList(L);cout<<endl;InvertDoubleChainList(L);cout<<endl<<"链表逆行后的结果:"<<endl<<endl;OutputDoubleChainList(L);break;}case 10:{//删除第i到第j个节点cout<<"****在链表中删除第i到第j个节点****"<<endl<<endl;cout<<"此操作前链表状态:" <<endl<<endl;OutputDoubleChainList(L);cout<<endl;cout<<"输入起点i值:"; cin>>start;cout<<"输入终点j值:"; cin>>end;cout<<endl<<endl;int len=LengthDoubleChainList(L);cout<<endl<<"删除第"<<start<<"到第"<<end<<"个节点:"<<endl;if(L->first&&(start>0&&start<-len)&&(end>0&&end<=len)&&start<=end){k=start;for(int i=0;i<end-start+1;i++)DeleteDoubleChainList(L,k);OutputDoubleChainList(L);}elsecout<<"起点或终点值有误"<<endl;break;}case 0:{//0--退出链表操作cout<<endl<<"******退出链表操作***********"<<endl;DestroyDoubleChainList(L);delete L;return 0;break;}}system("pause");system("cls");}}。