#include<stdio.h>
#define OK 1
#define ERROR -1
#define MAX_SIZE 100
typedef int Status ;
typedef int ElemType ;
typedef struct
{
ElemType Elem_array[MAX_SIZE] ;
int length ;
}sqlist;
int init(sqlist *L);
int insert(sqlist *L,int num,int e);
int del(sqlist *L,int num);
int display(sqlist *L);
int GetLength(sqlist *L);
int DelElem(sqlist *L,int a);
int main(void)
{
sqlist L;
init(&L);
printf("插入第一个元素\n");
insert(&L,0,1);
display(&L);
printf("长度为%d\n", GetLength(&L));
printf("插入第二个元素\n");
insert(&L,1,2);
printf("长度为%d\n", GetLength(&L));
display(&L);
printf("删除第一个元素\n");
del(&L, 0);
printf("长度为%d\n", GetLength(&L));
display(&L);
printf("删除与参数相同的第一个元素元素\n");
printf("插入两个相同个元素\n");
insert(&L,1,11);
insert(&L,2,11);
printf("长度为%d\n", GetLength(&L));
display(&L);
DelElem(&L,11);
printf("长度为%d\n", GetLength(&L));
display(&L);
system("pause");
}
/***********************************************************
*初始化线性表,申请内存空间
*fxqcn
*2011-10-2
***********************************************************/
int init(sqlist *L)
{
//L->Elem_array=(ElemType *)malloc(MAX_SIZE*sizeof(ElemType));
//if ( !L -> Elem_array ) return ERROR ;
//else {
L->length= 0 ; return OK ; //不知道为什么内存分配不成功,继续测试中
//}
}
/***********************************************************
*指定位置插入数据
*fxqcn
*2011-10-2
***********************************************************/
int insert(sqlist *L,int num,int e)
{
int i;
if(num>=MAX_SIZE) return ERROR;
for(i=num;i<L->length;i++)
L->Elem_array[i+1]=L->Elem_array[i];
L->Elem_array[num]=e;
L->length++;
return OK;
}
/***********************************************************
*指定位置删除数据
*fxqcn
*2011-10-2
***********************************************************/
int del(sqlist *L,int num)
{
int i;
if(num>=L->length) return ERROR;
for(i=num;i<L->length-1;i++)
L->Elem_array[i]=L->Elem_array[i+1];
L->length--;
return OK;
}
/***********************************************************
*显示线性表全部数据
*fxqcn
*2011-10-2
***********************************************************/
int display(sqlist *L)
{
int i;
if(L->length<=0) return ERROR;
for(i=0;i<L->length;i++)
printf("第%d个元素是:%d\n",i,L->Elem_array[i]);
return OK;
}
/***********************************************************
*显示线性表的长度
*fxqcn
*2011-10-3
***********************************************************/
int GetLength(sqlist *L)
{
return L->length;
}
/***********************************************************
*将指定的元素删除,只能删除第一个元素
*fxqcn
*2011-10-3
***********************************************************/
int DelElem(sqlist *L,int a)
{
int i,j;
if(L->length<=0) return ERROR;
for(i=0;i<L->length;i++)
{
if(a==L->Elem_array[i]) break;
}
if(L->Elem_array[i]==a)
{
for(j=i;j<L->length-1;j++)
L->Elem_array[j]==L->Elem_array[j+1];
L->length--;
}
}