c语言 完成顺序表的创建、初始化、查找、插入、删除、输出、撤销等操作

#include<stdio.h>
#include<stdlib.h>
#define ERROR 0
#define OK 1
#define overflow 2                                      //表示上溢
#define underflow 3                                     //表示下溢
#define NotPresent 4                                     //表示元素不存在
#define DupLicate 5                                     //表示有重复元素
typedef int ElemType;                                           //建立顺序表
typedef struct
{
int n;
int maxLength;
ElemType *element;
}SeqList;
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];                        //取出element[i]的值通过参数x返回
return OK;
}
Status Insert(SeqList *L,int i,ElemType m)
{   
int j;
if(!L->element)                         
return ERROR;
if(L->n==L->maxLength)                   //判断储存空间是否已满
return ERROR;
    for(j=L->n;j>i;j--)
L->element[j]=L->element[j-1];
L->element[i]=m;
L->n=L->n+1;
return OK;
}
Status Delete(SeqList *L,int i)
{   
int j;
if(i<0||i>L->n-1)                                 //下标i是否越界
return ERROR;
   if(!L->n)                                      //顺序表是否为空
return ERROR;
for(j=i;j<L->n-1;j++)
   L->element[j]=L->element[j+1];                    //从前往后逐个前移元素
L->n=L->n-1;                                    //表长减一
return OK;
}
Status Output(SeqList L)
{   
int i;
if(!L.n)
return ERROR;                                  //判断顺序表是否为空
for(i=0;i<L.n;i++)
    printf("%d",L.element[i]);
return OK;
}
void Destory(SeqList *L)
{
L->n=0;
L->maxLength=0;
free(L->element);
}
void main()
{
int i;
SeqList list;
Init(&list,10);                        //对顺序表进行初始化处理
for(i=0;i<=9;i++)            //利用for循环来插入数据,循环操作10次可插入10个数字
Insert(&list,i,i);
printf("\nthe Seqlist is:");
Output(list);                                   //输出插入数据,观察输入结果
Delete(&list,0);                               //删除操作,删除顺序表list中的0
    printf("\nthe  Delect Seqlist is:");
    Output(list);                                  //输出删除0后的数据
Destory(&list);                                 //执行撤销操作

}


这个主函数中利用的是for循环来实现数据的输入,所以会出现0123456789,因为for(i=0;i<=9;i++)规定了插入的数量所以只能出现0123456789,可以根据实际需要选择插入的数据,执行的结果如图所示。

c语言 完成顺序表的创建、初始化、查找、插入、删除、输出、撤销等操作