#ifndef SQLIST_H_INCLUDED
#define SQLIST_H_INCLUDED
#include "ds.h" //for Status,OK ...
#ifndef ElemType
#define ElemType int
#define ELEMTYPE_TAG
#endif
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct {
ElemType *elem;
int length;
int listsize;
} SqList;
Status InitList(SqList &L);
Status DestroyList(SqList &L);
Status ClearList(SqList &L);
bool ListEmpty(SqList L);
int ListLength(SqList L);
Status GetElem(SqList L, int i, ElemType &e);
template <typename T> bool equal(T a, T b)
{
return a==b;
}
int LocateElem(SqList L, ElemType e,
bool (*compare)(ElemType,ElemType)=equal<ElemType>);
Status ListInsert(SqList &L, int i, ElemType e);
Status ListDelete(SqList &L, int i, ElemType &e);
Status ListTraverse(SqList L, Status (*visit)(ElemType));
Status InitList(SqList &L)
{
L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
L.length=0;
L.listsize=100;
return OK;
}
Status DestroyList(SqList &L)
{
return ERROR;
}
Status ClearList(SqList &L)
{
return ERROR;
}
bool ListEmpty(SqList L)
{
if(L.length==0)
return 1;
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;
else return L.elem[i];
}
int LocateElem(SqList L, ElemType e, bool (*compare)(ElemType,ElemType))
{
for (int j=0; j<L.length; j++){
if ( compare(L.elem[j],e) ) return j+1;}
return 0;
}
Status ListInsert(SqList &L, int i, ElemType e)
{ if(i<1||i>L.listsize)
return ERROR;
else
{
L.length++;
int index;
for(index=L.length-1;index>=i-1;index--)
L.elem[index]=L.elem[index-1];
L.elem[index+1]=e;
return OK;
}
}
Status ListDelete(SqList &L, int i, ElemType &e)
{
if(i<1||i>L.listsize)
return ERROR;
else{
int index;
for(index=i-1;index<L.length-1;index++)
L.elem[index]=L.elem[index+1];
L.length--;
return OK;
}
//-------------------------------------
}
Status ListTraverse(SqList L, Status (*visit)(ElemType))
{
for (int j=0; j<L.length; j++)
if ( ! visit(L.elem[j]) ) return ERROR;
return OK;
}
#ifdef ELEMTYPE_TAG
#undef ElemType
#undef ELEMTYPE_TAG
#endif
#endif // SQLIST_H_INCLUDED