数据结构线性表实验.docx
文本预览下载声明
线性表的实验一、实验目的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)删除L的第5个元素;(11)输出顺序表L。源代码:head.h:#includestring.h#includectype.h#includemalloc.h //malloc( )#includelimits.h // INT ,MAX#includestdio.h //EOF,NULL#includestdlib.h //atoi( )#includeio.h //eof( )#includemath.h //floor( ),ceil( ),abs( )#includeprocess.h //exit( )#includeiostream //cout,cin//函数结果状态代码#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1//OVERFLOW 在 math.h 中已定义为3typedef int Status;typedef int Boolean; // 布尔类型main.cpp:#includehead.husing namespace std;typedef int ElemType;#define LIST_INIT_SIZE 100#define LISTINCREMENT 10typedef struct{ ElemType *elem; int length; int listsize;}Sqlist;Status InitList_Sq(Sqlist L){ //构造一个空的线性表L L.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType)); if(!L.elem) exit(OVERFLOW); L.length=0; L.listsize=LIST_INIT_SIZE; return OK;}Status ListRearInsert_Sq(Sqlist L,ElemType e){ ElemType *q=(L.elem[L.length]); *q=e; ++L.length; return OK;}Status ListPrint_Sq(Sqlist L){ for(int i=0;iL.length;i++) coutL.elem[i] ; coutendl; return OK;}Status ListLength_Sq(Sqlist L){ coutL.lengthendl; return OK;}Boolean ListEmpty_Sq(Sqlist L){ if(L.length==0) return true; else return false;}Status GetElem_Sq(Sqlist L,int i,ElemType e){ if(i1||iL.length+1) return ERROR; e=L.elem[i-1]; return OK;}Status GetElemIndex_Sq(Sqlist L,ElemType e){ for(int k=0;kL.length;k++) if(L.elem[k]==e) return k+1; return ERROR;}Status ListInsert_Sq(Sqlist L,int i,ElemType e){ if(i1||iL.length+1) return ERROR; if(L.length=L.listsize) { ElemType *newbase=(ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType)); if(!newbase) exit(OVERFLOW); L.elem=newbase; L.listsize+=LISTINCREMENT; } ElemType *q=(L.elem[i-1]); for(ElemType *p=(L.elem[L.length-1]);p=q;p--) *(p+1)=*p; *q=e; ++L.length;
显示全部