#include<stdio.h>
#include<iostream>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef char ElemType;
#define MAXSIZE 100
typedef struct
{
ElemType *elem;
int length;
}SqList;
Status InitList(SqList &L);
void DestroyList(SqList &L);
void ClearList(SqList &L);
int ListEmpty(SqList L);
int ListLength(SqList L);
Status GetElem(SqList L,int i,ElemType &e);
Status LocateElem(SqList L,ElemType e);
Status ListInsert(SqList &L,int i,ElemType e);
Status ListDelete(SqList &L,int i,ElemType &e);
void ListTraversals(SqList L);
int main(){
SqList L;
if(InitList(L))
cout << "线性表L创建成功" << endl;
ListInsert(L,1,'s');
if(ListInsert(L,1,'s'))
cout << "插入成功" << endl;
else
cout << "插入失败" << endl;
ListInsert(L,2,'m');
if(ListInsert(L,2,'m'))
cout << "插入成功" << endl;
else
cout << "插入失败" << endl;
ListInsert(L,3,'h');
if(ListInsert(L,3,'h'))
cout << "插入成功" << endl;
else
cout << "插入失败" << endl;
cout << "遍历线性表:";
void ListTraversals(SqList L);
cout << "线性表长度:" << ListLength(L) << endl;
ElemType e;
ListDelete(L, 1, e);
cout << "被删除的元素:" << e << endl;
cout << "被删除后线性表的长度:" << ListLength(L) << endl;
ElemType a='m';
int aa=LocateElem(L,a);
cout << "该元素" << a << "在表中序号为:" << aa << endl;
GetElem(L,1,e);
cout << "线性表L第一个元素:" << e << endl;
cout << "清空线性表" << endl;
ClearList(L);
ListEmpty(L);
if(ListEmpty(L))
cout << "线性表为空" << endl;
else
cout << "线性表非空" << endl;
DestroyList(L);
cout << "线性表已销毁" << endl;
return 0;
}
Status InitList(SqList &L)
{
L.elem = new ElemType[MAXSIZE];
if(!L.elem) exit(OVERFLOW) ;
L.length=0;
return OK;
}
void DestroyList(SqList &L){
if(L.elem)delete L.elem;
}
void ClearList(SqList &L){
L.length=0;
}
int ListEmpty(SqList L){
if (L.length == 0) return 1;
else return 0;
}
int ListLength(SqList L){
return (L.length);
}
Status GetElem(SqList L ,int i ,ElemType &e)
{
if(i < 1 || i > L.length) return ERROR;
e=L.elem[i-1];
return OK;
}
Status LocateElem(SqList L,ElemType e)
{
for(int i=0 ;i<L.length ;i++)
if(L.elem[i] == e) return i+1;
return 0;
}
Status ListInsert(SqList &L,int i,ElemType e)
{
if( (i<1)||(i>L.length+1) ) return ERROR;
if(L.length == MAXSIZE) return ERROR;
for(int j=L.length-1 ;j>=i-1 ;j--){
L.elem[j+1] = L.elem[j];
}
L.elem[i-1] = e;
++L.length;
return OK;
}
Status ListDelete(SqList &L,int i, ElemType &e)
{
if( (i<1) || (i>L.length) ) return ERROR;
e = L.elem[i-1];
for(int j=i ;j<=L.length-1 ;j++)
L.elem[j-1]=L.elem[j];
--L.length;
return OK;
}
void ListTraversals(SqList L){
for(int i=0;i<L.length;i++){
cout << L.elem[i] << endl;
}
}