# 数据结构（严蔚敏）之一——顺序表之c语言实现

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

#define LIST_INT_SIZE 100
#define LISTINCREMENT 10
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW   -1

typedef int ElemType;
typedef int Status;

typedef struct{
ElemType *elem;
int length;
int listsize;
}SqList;

Status InitList(SqList *L)//初始化线性表
{
L->elem = (ElemType *)malloc(LIST_INT_SIZE * sizeof(ElemType));
if(!L->elem) exit(OVERFLOW);
L->length = 0;
L->listsize = LIST_INT_SIZE;
return OK;
}

Status DestroyList(SqList *L)//销毁线性表
{
if(!L->elem) exit(ERROR);
free(L->elem);
L->elem = NULL;
return OK;
}

Status CreatList_Sq(SqList *L,int n)
{
int i;
printf("输入%d个整数:\n",n);
for(i=0;i<n;i++)
scanf("\n%d",&L->elem[i]);
return OK;
}

Status ClearList(SqList *L)//将线性表置为空表
{
if(!L->elem) return ERROR;
L->length = 0;
return OK;
}

Status ListEmpty(SqList *L)//判断线性表是否为空
{
if(!L->elem || L->length < 0 || L->length > L->listsize) return ERROR;
if(L->length == 0)
return TRUE;
else
return FALSE;
}

Status ListLength(SqList *L)//返回线性表长度
{
if(!L->elem || L->length < 0 || L->length > L->listsize)
return ERROR;
return L->length;
}

Status GetElem(SqList *L, int i, ElemType *e)//用e返回第i个元素的值
{
if(!L->elem || i < 0 || i > L->length) return ERROR;
*e = L->elem[i-1];
return OK;
}

Status LocatElem(SqList *L, ElemType e, int compare(void *, void *))
{
if(!L->elem) return ERROR;
int i;
for(i = 0; i < L->length; i++){
if(compare((ElemType *)e, (ElemType *)L->elem[i]) == TRUE)
return i+1;
}
return FALSE;
}

Status PriorElem(SqList *L, ElemType cur_e, ElemType *pre_e)
{
int i;
for (i = 1; i < L->listsize; i++) {
if (L->elem[i] == cur_e) {
*pre_e = L->elem[i-1]; /* 与Elemtype 相关*/
return OK;
}
}
return FALSE;
}

Status NextElem(SqList *L, ElemType cur_e, ElemType *next_e)
{
int i;
for (i=0; i < L->listsize - 1; i++) {
if (L->elem[i] == cur_e) {
*next_e = (int )L->elem[i+1];  /* 与Elemtype 相关*/
return OK;
}
}
return FALSE;
}

Status ListInsert(SqList *L, int i, ElemType e)//向线性表中插入元素
{
ElemType *newbase;
if(i < 1 || i > L->length+1) 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;
}
ElemType *q = &(L->elem[i-1]);
ElemType *p;
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)//从线性表中删除元素
{
if(i<1 || i>L->length) return ERROR;
ElemType *p = &(L->elem[i-1]);
e = *p;
ElemType *t = &(L->elem[L->length-1]);
for(++p; p <= t; ++p) *(p-1) = *p;
--L->length;
return OK;
}

Status ListTraverse(SqList *L, int (* visit)(void *))
{
int i;
for (i = 0; i < L->length; i++)
//如果失败
if (!visit((int *)L->elem[i]))/* 与Elemtype 相关*/
return ERROR;
return TRUE;
}

Status ListPrint(SqList *L)
{
int i;
if (!L->elem || L->length < 1 || L->length > L->listsize){
printf("empty!\n");
return ERROR;
}
for (i = 0; i < L->length; i++) {
printf("%5d ", L->elem[i]);
}
printf("\n");
return TRUE;
}

int main()
{
int i,n;
SqList a;
SqList *l = &a;
if(InitList(l)==-2)   printf("分配失败");
printf("\n输入要建立的线性表l的长度n:");//输入线性表得长度
scanf("%d",&n);
l->length=n;
printf("线性表的长度是:%d\n",l->length);
CreatList_Sq(l,n);//生成线性表
puts("原序：");//输出线性表中的元素
ListPrint(l);
puts("");
int ce, pe;
printf("请输入要查找前驱的元素：");
scanf("%d", &ce);
PriorElem(l, ce, &pe);
printf("%d的前驱是%d\n", ce, pe);
NextElem(l, ce, &pe);
printf("%d的后继是%d\n", ce, pe);

printf("请输入要插入的元素和插入位置：");
scanf("%d%d", &ce,&pe);
ListInsert(l, pe,ce);
printf("插入元素%d后的线性表为\n", ce);
ListPrint(l);

printf("请输入要删除元素位置:");
scanf("%d", &ce);
ListDelete(l, ce, pe);
printf("删除%d位置的<span style="font-family: Arial, Helvetica, sans-serif;">元素</span><span style="font-family: Arial, Helvetica, sans-serif;">后的线性表为\n", ce);</span>
	ListPrint(l);
DestroyList(l);//销毁顺序表
return 0;
}