#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
using namespace std;
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int ElemType;
typedef struct{
ElemType *elem;
int length;
int listsize;
int incrementsize;
}SqList;
void InitList(SqList &L,int maxsize=LIST_INIT_SIZE,int incresize=LISTINCREMENT);
void DestoryList(SqList &L);
void increment(SqList &L);
void ClearList(SqList &L);
bool ListEmpty(SqList L);
int ListLength(SqList &L);
void GetElem(SqList &L,int i,ElemType &e);
int LocateELem(SqList &L,ElemType &e);
void PriorElem(SqList &L,ElemType cur_e,ElemType &pre_e);
void NextElem(SqList &L,ElemType cure_e,ElemType &next_e);
void ListInsert(SqList &L,int i,ElemType e);
void ListDelete(SqList &L,int i,ElemType &e);
void visit(ElemType e);
void ListTraverse(SqList L,void (*vi)(ElemType));
int main()
{
SqList L;
InitList(L);
ElemType e1,e2;
for(int i=1;i<=5;i++)
ListInsert(L,i,i); //ListInsert(),ListTraverse(),visit()
cout<<"插入从五个元素后"<<endl;
ListTraverse(L,visit);
cout<<endl;
int l=ListLength(L); //ListLength()
cout<<"线性表长度:"<<l<<endl;
cout<<"是否为空:";
cout<<ListEmpty(L)<<endl; //ListEmpty()
ClearList(L); //ClearList()
cout<<"清空之后是否为空:";
cout<<ListEmpty(L)<<endl;
for(int i=1;i<=5;i++)
ListInsert(L,i,i);
PriorElem(L,2,e1); //PriorElem()
cout<<" 2 的前驱:"<<e1<<endl;
NextElem(L,2,e2); //NextElem()
cout<<" 2 的后继:"<<e2<<endl;
ListDelete(L,2,e1); //ListDelete()
cout<<"删除第二个元素之后"<<endl;
ListTraverse(L,visit);
return 0;
}
void InitList(SqList &L,int maxsize,int incresize)
{
L.elem=new ElemType[maxsize];
if(!L.elem)
{
cout<<"no enough memory!"<<endl;
exit(0);
}
L.length=0;
L.listsize=maxsize;
L.incrementsize=incresize;
}
void DestoryList(SqList &L)
{
delete [ ]L.elem;
L.elem=NULL;
L.length=0;
L.listsize=0;
}
void increment(SqList &L)
{
ElemType *a;
a=new ElemType[L.listsize+L.incrementsize];
for(int i=0;i<L.length;i++)
a[i]=L.elem[i];
delete[] L.elem;
L.elem=a;
L.listsize+=L.incrementsize;
}
void ClearList(SqList &L)
{
L.length=0;
}
bool ListEmpty(SqList L)
{
if(!L.length)
return true;
else
return false;
}
int ListLength(SqList &L)
{
return L.length;
}
void GetElem(SqList &L,int i,ElemType &e)
{
if(i<1 || i>L.length)
{
cout<<"error"<<endl;
exit(0);
}
e=L.elem[i];
}
int LocateELem(SqList &L,ElemType &e)
{
int i=1;
ElemType *p=L.elem;
while(i<=L.length && *p++!=e) ++i;
if(i<L.length) return i;
else return 0;
}
void PriorElem(SqList &L,ElemType cur_e,ElemType &pre_e)
{
if(*L.elem==cur_e)
{
cout<<"first element, no previous element"<<endl;
return ;
}
else
{
int i=2;
ElemType *p=L.elem+1;
while(i<=L.length && cur_e!=*p)
{
p++;
i++;
}
if(i>L.length)
{
cout<<"error"<<endl;
return ;
}
else
{
pre_e=*--p;
}
}
}
void NextElem(SqList &L,ElemType cure_e,ElemType &next_e)
{
int i=1;
ElemType *p=L.elem;
while(i<L.length && *p!=cure_e)
{
p++;
i++;
}
if(i==L.length)
{
cout<<"error"<<endl;
return ;
}
else
next_e=*++p;
}
void ListInsert(SqList &L,int i,ElemType e)
{
//i<=i<=L.length+1
if(i<1 || i>L.length+1)
{
cout<<"i值不合法"<<endl;
return ;
}
if(L.length>=L.listsize) increment(L);
ElemType *q=&(L.elem[i-1]);
ElemType *p=L.elem;
for(p=&(L.elem[L.length-1]);p>=q;--p)
*(p+1)=*p;
*q=e;
++L.length;
}
void ListDelete(SqList &L,int i,ElemType &e)
{
//在线性表中删除第i个元素,并用e返回其值
//i的合法值为1<=i<=L.length
ElemType *p=&(L.elem[i-1]);
e=*p;
ElemType *q=L.elem+L.length-1;
for(++p;p<=q;++p)
*(p-1)=*p;
--L.length;
}
void visit(ElemType e)
{
cout<<e<<" ";
}
void ListTraverse(SqList L,void (*vi)(ElemType))
{
ElemType *p;
int i;
p=L.elem;
for(i=1;i<=L.length;i++)
vi(*p++);
}