数据结构:线性表的顺序表

来源:互联网 发布:appserv linux 编辑:程序博客网 时间:2024/06/09 21:34

common.h

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2

#define LIST_INIT_SIZE 100 //线性表存储空间的初始分配量
#define LISTINCREMENT 10 //线性表存储空间的分配增量

//Status是函数的类型,其值是函数结果状态码
typedef int Status;

typedef int ElemType;

typedef struct
{
ElemType *elem; //存储空间基址
int length; //当前长度
int listsize; //当前分配的存储容量(以sizeof(ElemType)为单位)
}SqList;

LinearListH.h

class LinearList
{
private:

public:
LinearList();
~LinearList();
//操作结果:构造一个空的线性表L
Status InitList(SqList *L);
//初始条件:线性表L已存在
//操作结果:销毁线性表L
Status DestroyList(SqList *L);
//初始条件:线性表L已存在
//操作结果:将L表重置为空表
Status ClearList(SqList *L);
//初始条件:线性表L已存在
//操作结果:若L表为空表,则返回TRUE;否则返回FALSE
Status ListEmpty(SqList L);
//初始条件:线性表L已存在
//操作结果:返回L中数据元素个数
Status ListLength(SqList L);
//初始条件:线性表L已存在,1<=i<=ListLength(L);
//操作结果:用e返回L中第i个元素数据的值
Status GetElem(SqList L,int i,ElemType *e);
//初始条件:线性表L已存在,compare()是数据元素判定函数
//操作结果:返回L中第一个与e满足关系compare()的数据元素的位序。若这样的数据元素不存在,则返回值为0
Status LocateElem(SqList L,ElemType e,Status (*compare)(ElemType,ElemType));
//初始条件:线性表L已存在,
//操作结果:若cur_e是L的数据元素,且不是第一个,用pre_e返回他的前驱,否则操作失败,pre_e无定义
Status PriorElem(SqList L,ElemType cur_e,ElemType *pre_e);
//初始条件:线性表L已存在,
//操作结果:若cur_e是L的数据元素,且不是最后一个,用next_e返回他的后继,否则操作失败,next_e无定义
Status NextElem(SqList L,ElemType cur_e,ElemType *next_e);
//初始条件:线性表L已存在,1<=i<=ListLength(L)+1;
//操作结果:在L中第i个位置之前插入新的数据元素e,L的长度加1;
Status ListInsert(SqList *L,int i,ElemType e);
//初始条件:线性表L已存在,1<=i<=ListLength(L);
//操作结果:删除L中的第i个数据元素,并用e返回其值,L的长度减1;
Status ListDelete(SqList *L,int i,ElemType *e);
//初始条件:线性表L已存在,
//操作结果:依次对L的每个数据元素调用函数visit().一旦visit()失败,则操作失败
Status ListIraverse(SqList L,void (*vi)(ElemType *));

Status compare(ElemType e1,ElemType e2);
void visit(ElemType *e);

};


LinearListCpp.cpp
#include "common.h"
#include "LinearListH.h"

#include<iostream>
using namespace std;

LinearList::LinearList()
{
}

LinearList::~LinearList()
{
}

Status LinearList::InitList(SqList *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 LinearList::DestroyList(SqList *L)
{
free((*L).elem);
(*L).elem = NULL;
(*L).length = 0;
(*L).listsize = 0;
return OK;
}

Status LinearList::ClearList(SqList *L)
{
(*L).length = 0;
return OK;
}

Status LinearList::ListEmpty(SqList L)
{
if(L.length==0)
{
return TRUE;
}
else
{
return FALSE;
}
}

Status LinearList::ListLength(SqList L)
{
return L.length;
}

Status LinearList::GetElem(SqList L,int i,ElemType *e)
{
if(i<1||i>L.length)
{
exit(OVERFLOW);
}
*e = *(L.elem+i-1);
return OK;
}

Status LinearList::LocateElem(SqList L,ElemType e,Status (*compare)(ElemType,ElemType))
{
ElemType *p;
int i;
p = L.elem;
while (i<L.length&&!compare(*p++,e))
{
++i;
}
if (i<L.length)
{
return i;
}
else
{
return 0;
}
}

// 初始条件:顺序线性表L已存在
// 操作结果:若cur_e是L的数据元素,且不是第一个,则用pre_e返回它的前驱,
//否则操作失败,pre_e无定义
Status LinearList::PriorElem(SqList L,ElemType cur_e,ElemType *pre_e)
{
int i = 2;
ElemType *p = L.elem+1;
while (i<L.length&&*p!=cur_e)
{
p++;
i++;
}
if(i>L.length)
{
return INFEASIBLE;
}
else
{
*pre_e = *--p;
return OK;
}
}

//初始条件:顺序线性表L已存在
//操作结果:若cur_e是L的数据元素,且不是最后一个,则用next_e返回它的后继,
//否则操作失败,next_e无定义
Status LinearList::NextElem(SqList L,ElemType cur_e,ElemType *next_e)
{
int i =1;
ElemType *p = L.elem;
while (i<L.length&&*p!=cur_e)
{
i++;
p++;
}
if(i==L.length)
{
return INFEASIBLE;
}
else
{
*next_e = *++p;
return OK;
}
}

Status LinearList::ListInsert(SqList *L,int i,ElemType e)
{
ElemType *newbase,*p,*q;
if(i<1||i>(*L).length+1)
{
return ERROR;
}
if((*L).length>=(*L).listsize)
{
newbase = (ElemType*)realloc((*L).elem,((*L).listsize+LISTINCREMENT)*sizeof(ElemType));
if(!newbase)
{
exit(OVERFLOW);
}
(*L).elem = newbase;
(*L).listsize = (*L).listsize+LISTINCREMENT;
}
q = (*L).elem+i-1;
for(p=(*L).elem+(*L).length-1;p>=q;--p)
{
*(p+1) = *p;
}
*q = e;
++(*L).length;
return OK;
}

Status LinearList::ListDelete(SqList *L,int i,ElemType *e)
{
ElemType *p,*q;
if(i<1||i>(*L).length)
return ERROR;
p = (*L).elem+i-1;
*e = *p;
q = (*L).elem+(*L).length-1;
for (p++;p<q;++p)
{
*(p-1) = *p;
}
(*L).length--;
return OK;
}

Status LinearList::ListIraverse(SqList L,void (*visit)(ElemType *))
{
ElemType *p;
int i = 1;
p = L.elem;
for(i=0;i<L.length;i++)
{
visit(p++);
}
cout<<endl;
return OK;
}

Status LinearList::compare(ElemType e1,ElemType e2)
{
if (e1==e2)
{
return TRUE;
}
else
{
return FALSE;
}
}

void LinearList::visit(ElemType *e)
{
cout<<*e;
}

LinearListMain.cpp

#include "common.h"

#include "LinearListH.h"

#include<iostream>
using namespace std;

void main()
{
LinearList linearList;
SqList L;
Status i;
ElemType e,e0;
int j,k;
i = linearList.InitList(&L);
cout<<"初始化后L.elem="<<L.elem<<"L.length="<<L.length<<"L.listsize="<<L.listsize<<endl;
for (j = 1; j <= 5; j++)
{
i = linearList.ListInsert(&L,1,j);
}
cout<<"================"<<i<<endl;
cout<<"在表头插入1-5之后";
for( j = 1;j <= 5;j++)
{
cout<<*(L.elem+j-1)<<" ";
}
cout<<endl;
cout<<"初始化后L.elem="<<L.elem<<"L.length="<<L.length<<"L.listsize="<<L.listsize<<endl;
i = linearList.ListEmpty(L);
cout<<"是否为空(0:否;1:是)"<<i<<endl;
i = linearList.ClearList(&L);
cout<<"清空后L.elem="<<L.elem<<"L.length="<<L.length<<"L.listsize="<<L.listsize<<endl;
i = linearList.ListEmpty(L);
cout<<"是否为空(0:否;1:是)"<<i<<endl;
for (j = 1; j <= 10; j++)
{
linearList.ListInsert(&L,1,j);
}
cout<<"在表头插入1-10之后";
for( j = 1;j <= 10;j++)
{
cout<<*(L.elem+j-1)<<" ";
}
cout<<endl;
cout<<"插入后L.elem="<<L.elem<<"L.length="<<L.length<<"L.listsize="<<L.listsize<<endl;
linearList.ListInsert(&L,1,0);
cout<<"在表头插入0后";
for( j = 1;j <= 10;j++)
{
cout<<*(L.elem+j-1)<<" ";
}
cout<<endl;
cout<<"可能改变L.elem="<<L.elem<<"L.length="<<L.length<<"L.listsize="<<L.listsize<<endl;
linearList.GetElem(L,5,&e);
cout<<"第5个元素值:"<<e<<endl;
for(j = 1;j < 2;j++)
{
linearList.GetElem(L,j,&e0);
i = linearList.PriorElem(L,e0,&e);
if(i = OVERFLOW)
{
cout<<e0<<"无前驱"<<endl;
}
else
{
cout<<e0<<"前驱"<<e<<endl;
}
}
for(j = linearList.ListLength(L)-1;j <= linearList.ListLength(L) -1;j++)
{
linearList.GetElem(L,j,&e0);
i = linearList.NextElem(L,e0,&e);
if(i = OVERFLOW)
{
cout<<e0<<"无后继"<<endl;
}
else
{
cout<<e0<<"后继"<<e<<endl;
}
}
k = linearList.ListLength(L);
for(j = k+1;j>=k;j--)
{
i = linearList.ListDelete(&L,j,&e);
if(i==ERROR)
{
cout<<"删除第"<<j<<"个数据失败"<<endl;
}
else
{
cout<<"删除元素值"<<e<<endl;
}
}
cout<<"元素";
//linearList.ListIraverse(L,linearList.visit);
linearList.DestroyList(&L);
cout<<"销毁L后L.elem="<<L.elem<<"L.length="<<L.length<<"L.listsize="<<L.listsize<<endl;
}
0 0
原创粉丝点击