#include <stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define OVERFLOW -2
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int ElemType;
typedef int Status;
typedef struct{
// 存储空间基址
ElemType *elem;
// 当前长度
int length;
// 当前分配容量
int listsize;
}SqList;
//初始化线性表
Status InitList_Sq(SqList *L){
//不返回head,L即为结构体
// L是主函数传来的,并非定义指针所代表的,L大小100,固定
//1.构造空的线性表
L->elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
//2.分配失败
if(!L->elem)exit(OVERFLOW);
//3.空表长度为0
L->length=0;
//4.初始化存储容量
L->listsize=LIST_INIT_SIZE;
return OK;
}
//销毁线性表
Status DestroyList(SqList *L){
free(L);
return OK;
}
Status ClearList(SqList *L){
L->length=0;
return 1;
}
Status ListEmpty(SqList *L){
return (L->length==0);
}
Status ListLength(SqList *L){
return L->length;
}
Status getElem(SqList L,int i,ElemType *e){
if(i<0||i>L.length){
return 0;
}else{
*e=L.elem[i-1];
return OK;
}
}
Status lookup(SqList L,ElemType e){
int i;
ElemType *p;
p=L.elem;
for(i=0;i<L.length&&*p++!=e;)i++;
if(i<L.length)return i+1;
else return 0;
}
Status insert(SqList *L,int i,ElemType e){
int j;
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) exit(OVERFLOW);
L->elem=newbase;//新基址
L->listsize+=LISTINCREMENT;//扩容
}
for(j=L->length;j>=i;j--){
L->elem[j]=L->elem[j-1];//元素后移
}
L->elem[i-1]=e;
L->length++;
return 1;
}
Status deletes(SqList *L,int i,ElemType *e){
int j;
if((i<1)||i>L->length) return ERROR;
*e=L->elem[i-1];//取出
for(j=i-1;j<=L->length;j++){
L->elem[j]=L->elem[j+1];//元素前移
}
L->length--;
return OK;
}
Status print(SqList L){
int i;
for(i=0;i<L.length;i++){
printf("%d ",L.elem[i]);
}
printf("\n");
return 0;
}
Status scan(SqList *L){
int i,n;
printf("输入个数:");
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&L->elem[i]);
L->length++;
}
return 0;
}
int main(){
int k;
ElemType e;
SqList L;
if(InitList_Sq(&L)==OK) {
printf("successful!\n");
}else{
printf("defaule!\n");
}
printf("get numbers\n");
scan(&L);
print(L);
for(;;)
{
printf("\n");
printf("1.DestroyList 2.ClearList 3.ListEmpty 4.ListLength\n5.getElem 6.lookup 7.insert 8.deletes 9.exit\n");
scanf("%d",&k);
switch(k)
{
case 1:if(DestroyList(&L))printf("DestroyList is OK\n");break;
case 2:if(ClearList(&L))printf("ClearList is OK\n");break;
case 3:if(ListEmpty(&L))printf("The List is exist!\n");break;
case 4:printf("The ListLength is %d\n",ListLength(&L));break;
case 5:
{
int i;
printf("输入要查询的编号\n");
scanf("%d",&i);
if(getElem(L,i,&e))printf("第%d个是%d\n",i,e);break;
}
case 6:
{
int e,pos;
printf("输入要查找的值\n");
scanf("%d",&e);
pos=lookup(L,e);
if(pos>0)printf("位置是%d\n",pos);
else printf("not found\n");
break;
}
case 7:
{
int i,e;
printf("输入插入的位置和值\n");
scanf("%d %d",&i,&e);
insert(&L,i,e);print(L);break;
}
case 8:
{
int e,i;
printf("输入删除的位置\n");
scanf("%d",&i);
deletes(&L,i,&e);print(L);break;
}
case 9:break;break;
default:printf("error!\n");
}
}
printf("goodbye\n");
return 0;
}