#include <stdio.h>
#include <stdlib.h>
#include <time.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;
typedef struct {
ElemType *elem;
int length;
int listsize;
}SqList;
//操作结果:构造一个空的线性表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 ResizeList(SqList *L,int increment){
ElemType *newbase = (ElemType*)realloc(L->elem,(L->listsize+increment)*sizeof(ElemType));
if(!newbase) exit(OVERFLOW);
L->elem = newbase;
L->listsize += increment;
return OK;
}
//初始条件:线性表L已存在
//操作结果:销毁线性表L
Status DestroyList(SqList *L){
if(0==L->listsize) return ERROR;
free(L->elem);
L->elem = NULL;
L->length = 0;
L->listsize = 0;
return OK;
}
//初始条件:线性表L已存在
//操作结果:将L重置为空表
Status clearList(SqList *L){
if(0==L->listsize) return ERROR;
L->length = 0;
return OK;
}
//初始条件:线性表L已存在
//操作结果:若L为空表,则返回TRUE,否则返回FALSE
Status ListEmpty(SqList L){
return (L.length>0) ? FALSE : TRUE;
}
//初始条件:线性表L已存在
//操作结果:返回L中数据元素个数
Status ListLength(SqList L){
return L.length;
}
//初始条件:线性表L已存在,1<=i<=ListLength(L)
//操作结果:用e返回L中第i个数据元素的值
Status GetElem(SqList L,int i,ElemType *e){
if(i<1 || i>ListLength(L)) return ERROR;
*e = L.elem[i-1];
return OK;
}
//初始条件:线性表L已存在,1<=i<=ListLength(L)
//操作结果:用e改写L中第i个数据元素的值
Status SetElem(SqList L,int i,ElemT
线性表的顺序表示和实现:sqlist完整代码
最新推荐文章于 2023-08-20 15:53:22 发布