#include<stdio.h>
#include<stdlib.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Elemtype;
typedef struct{//定义一个顺序表的存储结构
Elemtype *elem;
int length;
int listsize;
}sqlist;
///*初始化顺序表*/
int Initlist_sq(sqlist *L){
L->elem=(Elemtype*)malloc(LIST_INIT_SIZE*sizeof(Elemtype));
if(!L->elem)exit(OVERFLOW);
L->length=0;
L->listsize=LIST_INIT_SIZE;
return OK;
}
//输入数据
void Input_data(sqlist *L){
int n;
printf("请输入要输入的数据数目:");
scanf("%d",&n);
for(int i=0;i<n;i++){
printf("请输入第%d个数:",i+1);
scanf("%d",&(L->elem[i]));
}
for(int j=0;j<n;j++){
printf("%d__",L->elem[j]);
}
printf("\n");
L->length=n;
}
//插入数据
int Listinsert_sq(sqlist *L,int i,Elemtype e){
if(i<1||i>L->length+1)return ERROR;
if(L->length>=L->listsize){
Elemtype *newbase;
newbase=(Elemtype *)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(Elemtype));
if(!newbase)exit(OVERFLOW);
L->elem=newbase;
L->listsize+=LISTINCREMENT;
}
Elemtype *q=&(L->elem[i-1]);
for(Elemtype *p=&(L->elem[L->length-1]);p>=q;--p)
*(p+1)=*p;
*q=e;
L->length++;
for(int j=0;j<L->length;j++){
printf("%d__",L->elem[j]);
}
printf("\n");
return OK;
}
//摧毁顺序表
void DestoryList(sqlist *L){
if(L->elem){
free(L->elem);
printf("destory list succeed!\n");
}
else
printf("destroy list failed!\n");
}
//清空顺序表
void CleanList(sqlist *L){
L->length=0;
printf("success!");
}
//测试顺序表是否为空
void TestEmpty(sqlist *L){
if(L->length==0)
printf("the list is empty!\n");
else
printf("the list is not empty!\n");
}
//返回元素在顺序表中的位置
int ReturnPosition(sqlist *L,Elemtype e){
for(int i=0;i<=L->length;i++){
if(L->elem[i]==e)
return i+1;
}
return ERROR;
}
//删除第i个位置上的元素
int DelectElem(sqlist *L,int i){
if(i<1||i>L->length+1)return ERROR;
Elemtype *q=&(L->elem[L->length-1]);
Elemtype *p=&(L->elem[i-1]);
Elemtype e=*p;
for(;p<q;p++){
*p=*(p+1);
}
q=0;
L->length--;
for(int j=0;j<L->length;j++){
printf("%d_",L->elem[j]);
}
printf("\n");
printf("the delected datum is %d\n",e);
return TRUE;
}
//顺序表逆序
void ListReversed(sqlist *L) {
printf("the reversed list is\n");
for(int i=0;i<L->length;i++){
printf("%d_",L->elem[L->length-i-1]);
}
printf("\n");
}
void main(){
int check=0;
int poe,poe2;
int delectdatum;
char dl,cl,te;
Elemtype e;
sqlist M;
check=Initlist_sq(&M);
if(check==1)printf("creat success!\n");
else printf("creat failed!\n");
Input_data(&M);
printf("输入想要插入的数据:");
scanf("%d",&e);
printf("输入想要插入的位置:");
scanf("%d",&poe);
check=0;
check=Listinsert_sq(&M,poe,e);
if(check==1)printf("insert success!\n");
else printf("insert failed!\n");
printf("do you want to test the list is or not empty?(Y or N)\n");
getchar();
scanf("%c",&te);
if(te=='Y')TestEmpty(&M);
printf("输入想要删除哪个位置的数据:");
scanf("%d",&poe2);
delectdatum=DelectElem(&M,poe2);
if(delectdatum==0)printf("delectElem failed!");
else
printf("delectElem succeed!\n");
ListReversed(&M);
printf("do you want to clean up the list?(Y or N)\n");
getchar();
scanf("%c",&cl);
if(cl=='Y')CleanList(&M);
printf("do you want to destory the list?(Y or N)\n");
getchar();
scanf("%c",&dl);
if(dl=='Y')DestoryList(&M);
}