#include <stdio.h>
#include <stdlib.h>
#include <malloc.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
typedef int Status;
typedef int ElemType;
/*C中的函数指针类型*/
typedef Status (*cmp)(ElemType x,ElemType i);
typedef void (*vis)();
struct SqList
{
ElemType *elem;
int length;
int listsize;
};
/*创建新的顺序表*/
Status InitList(SqList &L);
/*销毁顺序表*/
Status DestroyList(SqList &L);
/*如果顺序表为空TRUE 否则FALSE*/
Status ListEmpty(SqList &L);
/*返回顺序表长度*/
int ListLength(SqList &L);
/*返回指定位置的元素*/
ElemType GetElem(SqList &L,int i,ElemType *e);
/*定位指定元素,如果有返回第一个匹配的元素的位置*/
int LocateElem(SqList &L,ElemType e,cmp f);
/*int LocateElem_sq(SqList &L,ElemType e);*/
/*返回当前元素的前一个元素*/
ElemType PriorElem(SqList &L,int pos,ElemType *pre_e);
/*返回当前元素的后一个元素*/
ElemType NextElem(SqList &L,int pos,ElemType *next_e);
/*清空表*/
Status ClearList(SqList &L);
/*在指定位置插入元素*/
Status ListInsert(SqList &L,int i,ElemType e);
/*删除指定位置的元素*/
Status ListDelete(SqList &L, int i, ElemType e);
/*遍历顺序表*/
Status ListTraverse(SqList &L, vis v);
/*在顺序表中比较元素e*/
Status compare(ElemType x,ElemType i);
/*visit 函数在遍历表时返回当前元素*/
void visit(SqList &L);
void main()
{
int i,t ;
ElemType a;
int b;
cmp f;
SqList L;
ElemType *e;
if(!InitList(L))
{
printf("E001/n");
exit(ERROR);
}
printf("请输入数字,默认为5个/n");
for(i =1;i <= 5; i++)
{
int z[5] ;
scanf("%d,",&z[i-1]);
if (!ListInsert(L,i,z[i-1]))
{
printf("E002/n");
exit(ERROR);
}
}
printf("结果是:/n");
for(i = 1 ; i <= L.length;i++)
{
printf("%d/n",GetElem(L,i,e));
}
/*返回表长*/
printf("%d/n",ListLength(L));
a=3;
printf("%d/n",LocateElem(L,a, f));
DestroyList(L);
}
/*构造一个空表*/
Status 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 DestroyList(SqList &L)
{
if(!L.elem)exit(ERROR);
free(L.elem);
L.elem = NULL;
//L = NULL;
return OK;
}
/*如果顺序表为空TRUE 否则FALSE*/
Status ListEmpty(SqList &L)
{
if(L.elem == NULL) return TRUE;
else return FALSE;
}
/*返回顺序表长度*/
int ListLength(SqList &L)
{
if(!L.elem)exit(ERROR);
return L.length;
}
/*返回指定位置的元素*/
ElemType GetElem(SqList &L,int i,ElemType *e)
{
if(!L.elem || i > L.length || i < 1 )exit(ERROR);
/*e = L.elem + sizeof(ElemType)*(i-1);*/
e = L.elem + i - 1;
return *e;
}
/*定位指定元素,如果有返回第一个匹配的元素的位置*/
int LocateElem(SqList &L,ElemType e,cmp f)
{
//f = compare(ElemType x,int i);
int z,s;
ElemType *y;
if(!L.elem) exit(ERROR);
for(z=0;z<= L.length;z++)
{
/*y = L.elem + sizeof(ElemType)*z;*/
y = L.elem + z;
s = f(*y,e);
if (s == TRUE) return z;
}
return 0;
}
/*在顺序表中比较元素e*/
Status compare(ElemType x,ElemType i)
{
if (x == i) return TRUE;
return FALSE;
}
/*清空表将表中元素去掉,设置长度为0*/
Status ClearList(SqList &L)
{
int i;
if(!L.elem)exit(ERROR);
free(L.elem);
L.length = 0;
return OK;
}
/*在指定位置插入元素*/
Status ListInsert(SqList &L,int i,ElemType e)
{
ElemType *p,*q,*newbase;
if(i <1 || i > L.length +1)
{
printf("E004/n");
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 += 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 ListDelete(SqList &L, int i, ElemType e)
{
ElemType *p,*q;
if(i <1 || i > L.length +1)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;
}