采用顺序存储结构的线性表
1.初始化:动态分配空间
2.查找
3.插入
4.删除
5.输出
6.撤销
#include <stdio.h>
#include <stdlib.h>
//顺序表的定义
typedef int ElemType;
typedef struct seqList
{
int n;
int maxLength;
ElemType *element;
} SeqList;
//顺序表的初始化
#define ERROR 0
#define OK 1
#define Overflow 2
#define Underflow 3
#define NotPresent 4
#define Duplicate 5
typedef int Status;
Status Init (SeqList *L, int mSize)
{
L->maxLength = mSize;
L->n = 0;
L->element = (ElemType *)malloc(sizeof(ElemType)*mSize);
if(!L->element)
return ERROR;
return OK;
}
//顺序表的查找
Status Find(SeqList L,int i,ElemType *x)
{
if(i<0 || i> L.n-1)
return ERROR;
*x = L.element[i];
return OK;
}
//顺序表的插入
Status Insert(SeqList *L,int i,ElemType x)
{
int j;
if (i<-1 || i>L->n-1)
return ERROR;
if (L->n == L->maxLength)
return ERROR;
for (j = L->n-1;j>i;j--)
L->element[j+1] = L->element[j];
L->element[i+1] = x;
L->n = L->n + 1;
return OK;
}
//顺序表的删除
Status Delete(SeqList *L, int i)
{
int j;
if (i<0 || i>L->n-1)
return ERROR;
if (!L->n)
return ERROR;
for(j=i+1;j<L->n;j++)
{
L->element[j-1] = L->element[j];
}
L->n--;
return OK;
}
//顺序表的输出
Status Output(SeqList *L)
{
int i;
if(L->n == 0)
return ERROR;
for(i=0;i<= L->n -1 ;i++)
printf("%d ",L->element[i]);
printf("\n");
return OK;
}
//顺序表的撤销
void Destroy(SeqList *L)
{
L->n = 0;
L->maxLength = 0;
free(L->element);
}
int main(void)
{
int i;
SeqList list;
Init(&list,10);
for(i=0;i<10;i++)
Insert(&list,i-1,i);
Output(&list);
Delete(&list,0);
Output(&list);
Destroy(&list);
return 0;
}