#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 20
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int ElemType;
typedef struct
{
ElemType data[MAXSIZE];
int length;
} SqList;
Status GetElem(SqList *L,int i,ElemType e)
{
if(L->length==0||i<1||i>L->length)
{
return ERROR;
}
e=L->data[i-1];
return e;
}
Status ListInsert(SqList *L,int i,ElemType e)
{
int k;
if(L->length==MAXSIZE)
{
return ERROR;
}
if(i<1||i>L->length+1)
{
return ERROR;
}
if(i<=L->length)
{
for(k=L->length-1; k>=i-1; k--)
{
L->data[k+1]=L->data[k];
}
}
L->data[i-1]=e;
L->length++;
return OK;
}
Status ListDelete(SqList *L,int i,ElemType e)
{
int k;
if(L->length==0)
{
return ERROR;
}
if(i<1||i>L->length)
{
return ERROR;
}
e=L->data[i-1];
if(i<L->length)
{
for(k=i; k<L->length; k++)
{
L->data[k-1]=L->data[k];
}
}
L->length--;
return e;
}
Status PrintList(SqList *L)
{
for(int i=0;i<L->length;i++)
{
printf("%d ",L->data[i]);
}
printf("%\n");
return OK;
}
int main()
{
SqList l;
l.length=0;
for(int i=0;i<5;i++)
{
l.data[i]=i+1;
l.length++;
}
PrintList(&l);
ListInsert(&l,2,-99);
PrintList(&l);
int k;
k=GetElem(&l,2,k);
printf("获取第i个位置元素%d\n",k);
k=ListDelete(&l,2,k);
printf("删除第i个位置元素%d\n",k);
PrintList(&l);
return 0;
}
|