头文件 “cf1.h” # include<iostream.h> # include<malloc.h> # include<stdlib.h> # define LIST_INIT_SIZE 100 //线性表存储空间的初始分配量 # define LISTINCREAMENT 100 //线性表存储空间的分配增量 typedef struct{ int *elem; //存储空间基址,是数组的头指针 int length; //当前长度 int listsize; //当前分配的存储容量(以sizeof(int))为单位 }SqList; void InitList(SqList &L){ //构造一个空的线性表L L.elem=(int*)malloc(LIST_INIT_SIZE*sizeof(int)); if(!L.elem) exit(0); //存储分配失败 L.length=0; //空表的长度为0 L.listsize=LIST_INIT_SIZE; //初始存储容量 } void DestroyList(SqList &L){ //初始条件:线性表L已存在 //操作结果:销毁线性表 free(L.elem); L.length=0; L.listsize=0; L.elem=NULL; } void ClearList(SqList &L){ //操作三 //初始条件:线性表L已存在 //操作结果:将L重置为空表 free(L.elem); L.elem=(int*)malloc(LIST_INIT_SIZE*sizeof(int)); if(!L.elem) exit(0); //存储空间分配失败 L.length=0; //空表长度为0 L.listsize=LIST_INIT_SIZE; //当前存储空间分配的容量 } int ListEmpty(SqList L){ //操作二 //初始条件:线性表L存在 //操作结果:若L为空表,则返回1,否则返回0 if(L.length) return 0; return 1; } int ListLength(SqList L){ //初始条件:线性表L存在 //操作结果:返回L中数据元素的个数 return L.length; } void ListInsert(SqList &L,int i,int e){ //操作一 //初始条件:线性表L已存在,1<=i<=ListLength(L)+1 //操作结果:在L中第i个位置之前插入新的数据元素e,L的长度加1 if(i<1||i>L.length+1) cout<<"i的值不合法"<<endl; else{ if(L.length>=L.listsize){ //当前存储空间已满,增加分配容量 int *newbase=(int*)realloc(L.elem,(L.listsize+LISTINCREAMENT)*sizeof(int)); if(!newbase) exit(0); //存储空间分配失败 L.elem=newbase; //新基址 L.listsize+=LISTINCREAMENT; //增加存储容量 } int *q=&(L.elem[i-1]); //q为插入位置 for(int *p=&(L.elem[L.length-1]);p>=q;p--) *(p+1)=*p; //插入位置及之后的元素右移 *q=e;//插入e ++L.length; //表长增1 } } void ListDelete(SqList &L,int i,int &e){ //操作八 //初始条件:线性表L已存在且非空,1<=i<=ListLength(L) //操作结果:删除L的第i个数据元素,并用e返回其值,L的长度减1 if(i<1||i>L.length) cout<<"i值不合法"<<endl; else{ int *p=&(L.elem[i-1]); //p为被删除元素的位置 e=*p; //被删除元素的值赋给e int *q=L.elem+L.length-1; //表尾元素的位置 for(++p;p<=q;++p) *(p-1)=*p; //被删除元素之后的元素左移 --L.length; //表长减1 } } void GetElem(SqList L,int i,int &e){ //操作四 //初始条件:线性表L已存在,1<=i<=ListLength(L) //操作结果:用e返回L中第i个数据元素的值 if(i<1||i>ListLength(L)) cout<<"有误"<<endl; else e=L.elem[i-1]; } int compare(int e1,int e2){ //比较,如果相等,返回1,否则返回0 if(e1==e2) return 1; return 0; } int LocateElem(SqList L,int e,int(*compare)(int,int)){ //操作五 //初始条件:线性表L已存在;1<=i<=ListLength(L); //操作结果:返回L中第一个与e满足关系compare()的数据元素的位序,若这样的数据元素不存在,则返回0 int i=1;int *p=L.elem; while(i<=L.length&&!compare(*p++,e)) ++i; if(i<=L.length) return i; return 0; } void PriorElem(SqList L,int cur_e,int &pre_e){ //操作六 //初始条件:线性表L已存在 //操作结果:若cur_e是L的数据元素,且不是第一个,则用pre_e返回它的前驱;否则操作失败,pre_e无定义 int i=LocateElem(L,cur_e,compare); if(i==0) cout<<"不存在该数据元素"<<endl; else if(i==1) cout<<"是第一个元素,没有前驱结点"<<endl; else{ pre_e=L.elem[i-2]; } } void NextElem(SqList L,int cur_e,int &next_e){ //操作七 //初始条件:线性表L已存在 //操作结果:若cur_e是L的数据元素,且不是最后一个,则用next_e返回它的后继,否则操作失败,next_e无定义 int i=LocateElem(L,cur_e,compare); if(i==0) cout<<"不存在该元素"<<endl; else if(i==ListLength(L)) cout<<"是最后一个元素,无后继"<<endl; else next_e=L.elem[i]; } void visit(int e){ cout<<e<<" "; } void ListTraverse(SqList L,void(*visit)(int)){ //初始条件:线性表L已存在 //操作结果:依次对L的每个元素调用函数visit().一旦visit()失败,则操作失败 for(int i=0;i<ListLength(L);i++) visit(L.elem[i]); } 主文件 “cf2.cpp” # include"cf1.h" void main(){ cout<<"#####测试第一个操作########/n"; SqList L; InitList(L); cout<<ListEmpty(L)<<endl; cout<<ListLength(L)<<endl; ListInsert(L,1,1); ListInsert(L,2,2); ListInsert(L,3,3); ListTraverse(L,visit); cout<<endl<<ListLength(L)<<endl; cout<<"/n#####测试第二个操作########/n"; cout<<ListEmpty(L)<<endl; cout<<"/n#####测试第三个操作########/n"; ClearList(L); cout<<ListEmpty(L)<<endl; cout<<"/n#####测试第四个操作########/n"; ListInsert(L,1,1); ListInsert(L,2,2); ListInsert(L,3,3); int e; GetElem(L,1,e); cout<<e<<endl; GetElem(L,2,e); cout<<e<<endl; GetElem(L,0,e); GetElem(L,4,e); cout<<"/n#####测试第五个操作########/n"; cout<<LocateElem(L,0,compare)<<endl; cout<<LocateElem(L,1,compare)<<endl; cout<<LocateElem(L,2,compare)<<endl; cout<<LocateElem(L,3,compare)<<endl; cout<<LocateElem(L,4,compare)<<endl; cout<<"/n#####测试第六个操作########/n"; int pre_e; PriorElem(L,0,pre_e); PriorElem(L,1,pre_e); PriorElem(L,2,pre_e); cout<<pre_e<<endl; PriorElem(L,3,pre_e); cout<<pre_e<<endl; PriorElem(L,4,pre_e); cout<<"/n#####测试第七个操作########/n"; int next_e; NextElem(L,0,next_e); NextElem(L,1,next_e); cout<<next_e<<endl; NextElem(L,2,next_e); cout<<next_e<<endl; NextElem(L,3,next_e); NextElem(L,4,next_e); cout<<"/n#####测试第八个操作########/n"; ListDelete(L,0,e); ListDelete(L,1,e); cout<<e<<endl; ListTraverse(L,visit); cout<<ListLength(L)<<endl; DestroyList(L); } 输出结果: