# 线性表顺序存储结构实现

2015/07/15 15:13

#include<stdio.h>

#define MAXSIZE 20
typedef int ElemType;
typedef struct
{
ElemType data[MAXSIZE];
int length;
}SqList;

int get_elem(SqList *L, int i, ElemType *e)
{
if(L->length==0 || i<1 || i>L->length)
return 0;
*e=L->data[i-1];
return 1;
}

int insert_elem(SqList *L, int i, ElemType e)
{
int j;
if (L->length ==0&&i==0){
L->data[0]=e;
L->length++;
return 1;
}
if(L->length==MAXSIZE || i<0 || i>L->length)
return 0;
for(j=L->length;j>i;j--){
L->data[j]=L->data[j-1];
}
L->data[i]=e;
L->length++;
return 1;
}

int delete_elem(SqList *L, int i, ElemType *e)
{
if(L->length==0|| i<0 || i>L->length-1)
return 0;
*e=L->data[i];
for( ;i<L->length-1;i++)
L->data[i]=L->data[i+1];
L->length--;
return 1;
}

int init_sqlist(SqList *L)
{
L->length=0;
}

int test_sqlist(SqList *L)
{
int i=0;
int data=0;
int *e;

printf("input the damn data, split by return, end by -1\n");
do {
scanf("%d",&data);
insert_elem(L,i++,data);
} while (data!=-1);

printf("the list is:\n");
print_sqlist(L);

printf("input the position of the number you will delete:\n");
scanf("%d",&data);
delete_elem(L,data,e);
printf("the number you deleted is %d\n",*e);

printf("now the list is:\n");
print_sqlist(L);

return 1;
}

int print_sqlist(SqList *L)
{
int j;
for(j=0;j<L->length-1;j++)
printf("the %dnd data is %d\n",j,L->data[j]);

return 1;
}

int main()
{
SqList L1;
SqList *p;
p=&L1;
init_sqlist(p);
test_sqlist(p);
return 1;
}

0
4 收藏

0 评论
4 收藏
0