一点点顺序表的用法,发此备份。
//ADT List {
//数据对象:D={ ai | ai ∈ElemSet, i = 1, 2, ..., n, n≥0 }
//数据关系:R1={ <ai-1 , ai > | ai-1, ai ∈D, i = 2, ..., n }
//基本操作:
//InitList(&L); 构造一个空的线性表
//DestroyList( &L ); 销毁线性表L
//ClearList( &L ); 将L重置为空表
//ListEmpty( L ); 判断L是否为空表,是则返回TRUE
//GetElem( L, i , &e ); 用e返回L中第i个数据元素的值
//LocateElem(L,e,compare());定位首次与e满足compare关系的元素
//ListInsert( &L, i , e ); 在L中第i个位置之前插入元素e
//ListDelete( &L, i ); 删除L的第i个元素,并用e返回
//} ADT List
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status; //Status 是函数返回值类型,其值是函数结果状态代码。
typedef int ElemType; //ElemType 为可定义的数据类型,此设为int类型
#define MAXSIZE 100 //顺序表可能达到的最大长度
typedef struct {
ElemType *elem; //存储空间的基地址
int length; //当前长度
} SqList;
Status InitList(SqList &L) //算法2.1 顺序表的初始化
{
//构造一个空的顺序表L
L.elem = new ElemType[MAXSIZE]; //为顺序表分配一个大小为MAXSIZE的数组空间
if(!L.elem)
exit(OVERFLOW); //存储分配失败退出
L.length = 0; //空表长度为0
return OK;
}
void DestroyList(SqList &L)
{
if(L.elem)
delete []L.elem; //释放存储空间
}
int ListLength(SqList L)
{
/****在此下面完成代码***************/
return L.length;
/***********************************/
}
bool ListEmpty(SqList L)
{
/****在此下面完成代码***************/
if(L.length==0)
return OK;
else
return ERROR;
/***********************************/
}
Status GetElem(SqList L, int i, ElemType & e) //算法2.2 顺序表的取值
{
/****在此下面完成代码***************/
if(i<1||i>L.length)
return ERROR;
e=L.elem[i-1];
return OK;
/***********************************/
}
int LocateElem(SqList L, ElemType e) //算法2.3 顺序表的查找
{
/****在此下面完成代码***************/
int i;
for(i=0;i<L.length;i++)
if(L.elem[i]==e)return i+1; //length从1开始,elem从0开始
return 0;
/***********************************/
}
Status ListInsert(SqList & L, int i, ElemType e) //算法2.4 顺序表的插入
{
/****在此下面完成代码***************/
if((i<1)||(i>L.length+1)) //判断i取值是否合理
return ERROR;
if(L.length==MAXSIZE) //L.length长度满了
return ERROR;
int j=L.length-1; //L.length为最后一个数据所在位置
while(j>=i-1)
{
L.elem[j+1]=L.elem[j]; //将i-1之后的元素依次后移 从尾部开始
j--;
}
L.elem[i-1]=e;
++L.length; //总体长度加一
return OK;
/***********************************/
}
Status ListDelete(SqList & L, int i) //算法2.5 顺序表的删除
{
/****在此下面完成代码***************/
int j;
if((i<1)||(i>L.length))return ERROR; //判断i取值是否合理
for(j=i;j<=L.length-1;j++)
L.elem[j-1]=L.elem[j]; //将i-1之后的元素依次前移 从i-1开始前移
--L.length;
return OK;
/***********************************/
}
void ListPrint(SqList L)
{
for(int i = 0; i < L.length; i++)
cout << L.elem[i] << ((i == L.length - 1) ? '\n' : ' ');
}
int main()
{
int i;
ElemType e;
SqList L;
string op;
InitList(L);
while(cin >> op) {
if(op == "Empty")
cout << (ListEmpty(L) ? "Empty" : "Not empty") << endl;
else if(op == "Insert") {
cin >> i >> e;
if(ListInsert(L, i, e) == ERROR)
cout << "Insert failed" << endl;
else
ListPrint(L);
} else if(op == "Length") {
cout << "List length is " << ListLength(L) << endl;
} else if(op == "GetElem") {
cin >> i;
if(GetElem(L, i, e) == ERROR)
cout << "Out of index" << endl;
else
cout << "The elem at position " << i << " is " << e << endl;
} else if(op == "LocateElem") {
cin >> e;
i = LocateElem(L, e);
if(i == 0)
cout << e << " is not found in list" << endl;
else
cout << e << " is found at the position " << i << endl;
} else if(op == "Delete") {
cin >> i;
if(ListDelete(L, i) == ERROR)
cout << "Delete failed" << endl;
else
ListPrint(L);
}
}
DestroyList(L);
return 0;
}