//线性表顺序存储
#include<stdio.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
#define MAXSIZE 20
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 OK;
}
Status ListInsert(SqList *L,int i,ElemType e)
{
int j;
if(L->length == MAXSIZE)
return ERROR;
if(i<1 || i>L->length + 1)
return ERROR;
if(i<= L->length)
{
for(j = L->length - 1 ;j>=i-1;j--)
{
L->data[j+1]=L->data[j];
}
}
L->data[i -1] = e;
printf("insert=%d\n",L->data[i - 1]);
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 OK;
}
int main()
{
int i,data;
SqList L;
for(i = 0;i<MAXSIZE;i++)
{
L.data[i] = i;
}
L.length = MAXSIZE;
GetElem(L,3,&data);
printf("data=%d\n",data);
ListDelete(&L,3,&data) ;
printf("delete result\n");
for(i = 0;i<L.length;i++)
printf(" %d",L.data[i]);
printf("\n");
printf("insert result\n");
ListInsert(&L,3,22);
for(i = 0;i<MAXSIZE;i++)
printf(" %d",L.data[i]);
printf("\n");
}
//学习大话数据结构 加上测试程序
数据结构之线性表顺序存储
最新推荐文章于 2024-04-09 19:52:44 发布