# include "stdio.h"
# include "stdlib.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -1
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int Status;
typedef int ElemType;
typedef struct
{
ElemType *elem;//存储空间基址
int length;
int listsize;//当前分配的存储容量
}SqList;
//构造空线性表
Status InitList_Sq(SqList *L)
{
L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(L->elem==0)exit(OVERFLOW);
L->length=0;
L->listsize=LIST_INIT_SIZE;
return OK;
}
//写入数据
void InputData(SqList *L)
{
int n,i;
printf("input n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("No.%d:",i+1);
scanf("%d",&L->elem[i]);
}
L->length=n;
}
//遍历
void ListPrint(SqList L)
{
int i;
printf("The list is :");
for(i=0;i<L.length;i++)
{
printf("%d ",L.elem[i]);
}
printf("\n");
}
//判空
Status ListEmpty(SqList L)
{
return L.length==0?TRUE:FALSE;
}
//输出线性表元素个数
int ListLength(SqList L)
{
return L.length;
}
//清空线性表
Status ClearList(SqList *L)
{
L->length=0;
return OK;
}
//销毁线性表
void DestroyList(SqList *L)
{
free(L->elem);//非free(L)
L->length = 0;
L->listsize = 0;//存储容量为0
L->elem = NULL;//数据为空
}
//从线性表中获取第i个元素
Status GetElem(SqList L,int i,ElemType *e)
{
if(i<1||i>L.length)
return ERROR;
*e = L.elem[i-1];
return OK;
}
//查找线性表中的数据
int LocateElem_Sq(SqList L,ElemType e)
{
//输入一个数在线性表中找到他的位置
int i;
ElemType *p;
i=1;
p=L.elem;
while(i<=L.length && *p!=e)
{
++i;
p++;
}
if(i<=L.length)return i;
else
return 0;
}
//删除线性表中的数据
Status ListDelete_Sq(SqList *L,int i,ElemType *e)
{//删除第i个元素并返回其值e
ElemType *p;
ElemType *q;
if(i<1||(i>L->length))
return ERROR;
p=&(L->elem[i-1]);
*e=*p;//找到需要删除的值传给*e
q=L->elem+L->length-1;//表尾元素的位置
for(++p;p<=q;++p)
*(p-1)=*p;
--L->length;
return OK;
}
//线性表中插入数据
Status ListInsert_Sq(SqList *L,int i,ElemType e)
{
ElemType *q;
ElemType *p;
ElemType *newbase;
if(i<1||i>L->length+1)//插入位置不符合条件
return ERROR;
if(L->length>=L->listsize)//当前存储空间已满,需要增加分配
{
newbase = (ElemType *)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(ElemType));
if(newbase==0)exit(OVERFLOW);
L->elem=newbase;
L->listsize+=LISTINCREMENT;
}
q = &(L->elem[i-1]);//q为插入位置
for(p = &(L->elem[L->length-1]);p>=q;--p)*(p+1)=*p;//插入位置之后元素后移
*p=e;
++L->length;
return OK;
}
# include "stdio.h"
# include "stdlib.h"
# include "SqList.h"
void main()
{
SqList L;
int choice=1;
if(InitList_Sq(&L)==OK)
printf("success!\n");
else
printf("fail!\n");
while(choice!=0)
{
//meau
system("cls");
printf("1.InputDate\t2.ListPrint\t3.ListEmpty\t4.ListLength\t5.ClearList\n");
printf("6.DestroyList\t7.GetElem\t8.LocateElem\t9.ListDelete\t10.ListInsert\n");
printf("0.Exit\n");
//input choice
printf("Input your choice:");
scanf("%d",&choice);
//action
switch(choice)
{
case 0:
printf("ByeBye!\n");
break;
case 1:
InputData(&L);
break;
case 2:
ListPrint(L);
break;
case 3:
if(ListEmpty(L)==TRUE)
printf("The list is empty!\n");
else
printf("The list is not empty!\n");
break;
case 4:
{
int len;
len = ListLength(L);
printf("The length is %d.\n",len);
break;
}
case 5:
if(ClearList(&L)==OK)
printf("Clear!\n");
break;
case 6:
DestroyList(&L);
break;
case 7:
{
int pos;
ElemType e;
printf("Input the position:");
scanf("%d",&pos);
if(GetElem(L,pos,&e)==OK)
printf("The No.%d element is %d.",pos,e);
else
printf("The position is invalid.\n");
break;
}
case 8:
{
ElemType e;
int pos=LocateElem_Sq(L,e);
printf("Input the number:");
scanf("%d",&e);
if(pos>=0)
printf("Found! Tts position is %d.\n",pos+1);
else
printf("Not found!\n");
break;
}
case 9:
{
ElemType *e;
int i;
if((ListDelete_Sq(&L,i,&e))==TRUE)
printf("Have deleted!\n");
else
printf("Have not deleted!\n");
break;
}
case 10:
{
int i;
ElemType e;
printf("Input the position:");
scanf("%d",i);
printf("Input a number:");
scanf("%d",e);
if(ListInsert_Sq(&L,i,e)==OK)
printf("Have inserted!\n");
else
printf("Not inserted!\n");
break;
}
}
system("pause");
}
DestroyList(&L);
}