數據結構:線性表的順序表

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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章