#include <stdio.h>
#include <stdlib.h>
# include <malloc.h>
#define MAXSIZE 100
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int ElemType;
typedef int Status;
typedef struct
{
ElemType *pList;
int sqSize;
int sqLength;
}SqList;
Status InitList(SqList *L);
Status CreateList(SqList *L);
Status PrintList(SqList *L);
Status InsertList(SqList *L,int i,ElemType e);
Status DeleteSqList(SqList *L,int i,ElemType *e);
Status InitList(SqList *L)
{
L->pList = (SqList *)malloc(sizeof(SqList));
if(L->pList == NULL)
{
printf("内存空间分配失败!");
}
else
{
L->sqLength = 0;
}
return OK;
}
Status CreateList(SqList *L)
{
int n,i;
printf("请输入要输入的数据的个数:");
scanf("%d",&n);
if(n>MAXSIZE)
{
printf("超出最大容量,请重新输入");
}
printf("请输入:");
for( i=0;i<n;i++)
{
scanf("%d",&L->pList[i]);
L->sqLength++;
}
return OK;
}
Status PrintList(SqList *L)
{
int j;
for(j=0;j<L->sqLength;j++)
{
printf("%d ",L->pList[j]);
}
printf("\n");
return OK;
}
Status InsertList(SqList *L,int i,ElemType e)
{
int index;
if(L->sqLength == L->sqSize || i>L->sqLength )
{
return FALSE;
}
for(index=L->sqLength-1;index>=i-1;index--)
{
L->pList[index+1] = L->pList[index];
}
L->pList[i-1] = e;
L->sqLength++;
return OK;
}
Status DeleteSqList(SqList *L,int i,ElemType *e)
{
int index;
if(L->sqLength == NULL || i<1 || i>L->sqLength)
return FALSE;
*e = L->pList[i-1];
if(i<L->sqLength)
{
for(index=i;index<L->sqLength;index++)
{
L->pList[index-1] = L->pList[index];
L->sqLength--;
}
return OK;
}
}
int main()
{
SqList L ;
int e;
InitList(&L);
CreateList(&L);
printf("-----原顺序表-----");
printf("\n");
PrintList(&L);
InsertList(&L,3,0);
printf("-----插入元素后的顺序表为-----");
printf("\n");
PrintList(&L);
DeleteSqList(&L,5,&e);
printf("-----删除元素后的顺序表为-----");
printf("\n");
PrintList(&L);
}