欢迎来到天天文库
浏览记录
ID:30885485
大小:525.50 KB
页数:40页
时间:2019-01-03
《数据结构线性表实验》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、线性表的实验一、实验目的1.掌握用C/C++语言调试程序的基本方法。2.掌握线性表的基本运算,如插入、删除等。二、实验内容1.1实现顺序表各种基本操作的算法编写程序实现顺序表的各种基本运算,并在此基础上设计一个主程序完成如下功能:(1)初始化顺序表L;(2)依次在L尾部插入元素12,-21,13,4,8;(3)输出顺序表L;(4)输出顺序表L长度;(5)判断顺序表L是否为空;(6)输出顺序表L的第4个元素;(7)输出元素-21的位置;(8)在L的第4个元素前插入元素0;(9)输出顺序表L;(10
2、)删除L的第5个元素;(11)输出顺序表L。源代码:head.h:#include#include#include//malloc()#include//INT,MAX#include//EOF,NULL#include//atoi()#include//eof()#include//floor(),ceil(),abs()#include3、s.h>//exit()#includeviostream>//cout,cin〃函数结果状态代码#defineTRUE1#defineFALSE0#defineOK1#defineERROR0#defineINFEASIBLE-1//OVERFLOW在math.h中己定义为3typedefintStatus;typedefintBoolean;//布尔类型main.cpp:#include,,head.hMusingnamespacestd;typedefintElemType;#define4、LISTJNIT_SIZE100#defineLISTINCREMENT10typedefstruct{ElemType*elem;intlength;intlistsize;}Sqlist;StatuslnitList_Sq(Sqlist&L){〃构造一个空的线性表LL.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));if(!L.elem)exit(OVERFLOW);L.length=0;L.listsize二LIST_INIT5、_SIZE;returnOK;}StatusListRearlnsert_Sq(Sqlist&L,ElemTypee)ElemType*q=&(L.elem[L.length]);*q=e;++L」ength;returnOK;}StatusListPrint_Sq(Sqlist&L)for(inti=0;i6、vendl;returnOK;}BooleanListEmpty_Sq(Sqlist&L)讦(L.length==O)returntrue;elsereturnfalse;}StatusGetElem_Sq(Sqlist&L」nti,ElemType&e){讦(i7、8、i>L.length+l)returnERROR;e=L.elem[i-l];returnOK;}StatusGetElemlndex_Sq(Sqlist&L,ElemTypee){forjintk=O;k9、++)if(L.elem[k]==e)returnk+1;returnERROR;StatusListInsert_Sq(Sqlist&L」nti,ElemTypee)讦(i10、11、i>L.length+l)returnERROR;讦(L」ength>=L」istsize){ElemType*newbase=(ElemType*)realloc(Lelem,(Llistsize+LISTINCREMENT)*sizeof(ElemType));if(inewbase)exit(OVERFLOW)12、;L.elem二newbase;L」istsize+=LISTINCREMENT;}ElemType*q=&(L.elem[i-1]);for(ElemType*p二&(L.elem[L」ength-l]);p>=q;p-)*(p+l)=*p;*q=e;++L」ength;returnOK;}StatusListDelete_Sq(Sqlist&L,inti,ElemType&e)if(i13、14、i>L.length)returnERROR;ElemType*p=&(L.elem[i-1]);e
3、s.h>//exit()#includeviostream>//cout,cin〃函数结果状态代码#defineTRUE1#defineFALSE0#defineOK1#defineERROR0#defineINFEASIBLE-1//OVERFLOW在math.h中己定义为3typedefintStatus;typedefintBoolean;//布尔类型main.cpp:#include,,head.hMusingnamespacestd;typedefintElemType;#define
4、LISTJNIT_SIZE100#defineLISTINCREMENT10typedefstruct{ElemType*elem;intlength;intlistsize;}Sqlist;StatuslnitList_Sq(Sqlist&L){〃构造一个空的线性表LL.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));if(!L.elem)exit(OVERFLOW);L.length=0;L.listsize二LIST_INIT
5、_SIZE;returnOK;}StatusListRearlnsert_Sq(Sqlist&L,ElemTypee)ElemType*q=&(L.elem[L.length]);*q=e;++L」ength;returnOK;}StatusListPrint_Sq(Sqlist&L)for(inti=0;i6、vendl;returnOK;}BooleanListEmpty_Sq(Sqlist&L)讦(L.length==O)returntrue;elsereturnfalse;}StatusGetElem_Sq(Sqlist&L」nti,ElemType&e){讦(i7、8、i>L.length+l)returnERROR;e=L.elem[i-l];returnOK;}StatusGetElemlndex_Sq(Sqlist&L,ElemTypee){forjintk=O;k9、++)if(L.elem[k]==e)returnk+1;returnERROR;StatusListInsert_Sq(Sqlist&L」nti,ElemTypee)讦(i10、11、i>L.length+l)returnERROR;讦(L」ength>=L」istsize){ElemType*newbase=(ElemType*)realloc(Lelem,(Llistsize+LISTINCREMENT)*sizeof(ElemType));if(inewbase)exit(OVERFLOW)12、;L.elem二newbase;L」istsize+=LISTINCREMENT;}ElemType*q=&(L.elem[i-1]);for(ElemType*p二&(L.elem[L」ength-l]);p>=q;p-)*(p+l)=*p;*q=e;++L」ength;returnOK;}StatusListDelete_Sq(Sqlist&L,inti,ElemType&e)if(i13、14、i>L.length)returnERROR;ElemType*p=&(L.elem[i-1]);e
6、vendl;returnOK;}BooleanListEmpty_Sq(Sqlist&L)讦(L.length==O)returntrue;elsereturnfalse;}StatusGetElem_Sq(Sqlist&L」nti,ElemType&e){讦(i7、8、i>L.length+l)returnERROR;e=L.elem[i-l];returnOK;}StatusGetElemlndex_Sq(Sqlist&L,ElemTypee){forjintk=O;k9、++)if(L.elem[k]==e)returnk+1;returnERROR;StatusListInsert_Sq(Sqlist&L」nti,ElemTypee)讦(i10、11、i>L.length+l)returnERROR;讦(L」ength>=L」istsize){ElemType*newbase=(ElemType*)realloc(Lelem,(Llistsize+LISTINCREMENT)*sizeof(ElemType));if(inewbase)exit(OVERFLOW)12、;L.elem二newbase;L」istsize+=LISTINCREMENT;}ElemType*q=&(L.elem[i-1]);for(ElemType*p二&(L.elem[L」ength-l]);p>=q;p-)*(p+l)=*p;*q=e;++L」ength;returnOK;}StatusListDelete_Sq(Sqlist&L,inti,ElemType&e)if(i13、14、i>L.length)returnERROR;ElemType*p=&(L.elem[i-1]);e
7、
8、i>L.length+l)returnERROR;e=L.elem[i-l];returnOK;}StatusGetElemlndex_Sq(Sqlist&L,ElemTypee){forjintk=O;k9、++)if(L.elem[k]==e)returnk+1;returnERROR;StatusListInsert_Sq(Sqlist&L」nti,ElemTypee)讦(i10、11、i>L.length+l)returnERROR;讦(L」ength>=L」istsize){ElemType*newbase=(ElemType*)realloc(Lelem,(Llistsize+LISTINCREMENT)*sizeof(ElemType));if(inewbase)exit(OVERFLOW)12、;L.elem二newbase;L」istsize+=LISTINCREMENT;}ElemType*q=&(L.elem[i-1]);for(ElemType*p二&(L.elem[L」ength-l]);p>=q;p-)*(p+l)=*p;*q=e;++L」ength;returnOK;}StatusListDelete_Sq(Sqlist&L,inti,ElemType&e)if(i13、14、i>L.length)returnERROR;ElemType*p=&(L.elem[i-1]);e
9、++)if(L.elem[k]==e)returnk+1;returnERROR;StatusListInsert_Sq(Sqlist&L」nti,ElemTypee)讦(i10、11、i>L.length+l)returnERROR;讦(L」ength>=L」istsize){ElemType*newbase=(ElemType*)realloc(Lelem,(Llistsize+LISTINCREMENT)*sizeof(ElemType));if(inewbase)exit(OVERFLOW)12、;L.elem二newbase;L」istsize+=LISTINCREMENT;}ElemType*q=&(L.elem[i-1]);for(ElemType*p二&(L.elem[L」ength-l]);p>=q;p-)*(p+l)=*p;*q=e;++L」ength;returnOK;}StatusListDelete_Sq(Sqlist&L,inti,ElemType&e)if(i13、14、i>L.length)returnERROR;ElemType*p=&(L.elem[i-1]);e
10、
11、i>L.length+l)returnERROR;讦(L」ength>=L」istsize){ElemType*newbase=(ElemType*)realloc(Lelem,(Llistsize+LISTINCREMENT)*sizeof(ElemType));if(inewbase)exit(OVERFLOW)
12、;L.elem二newbase;L」istsize+=LISTINCREMENT;}ElemType*q=&(L.elem[i-1]);for(ElemType*p二&(L.elem[L」ength-l]);p>=q;p-)*(p+l)=*p;*q=e;++L」ength;returnOK;}StatusListDelete_Sq(Sqlist&L,inti,ElemType&e)if(i13、14、i>L.length)returnERROR;ElemType*p=&(L.elem[i-1]);e
13、
14、i>L.length)returnERROR;ElemType*p=&(L.elem[i-1]);e
此文档下载收益归作者所有