#include<stdio.h>
#include<stdlib.h>
typedef struct
{
int* i;
int length;
}sqlist;
void newm();
bool newsqlist(sqlist& L);
bool Insert(sqlist& L, int i, int j);
bool Get(sqlist &L, int i, int& e);
bool deletem(sqlist& L, int i);
int locate(sqlist L, int i);
void print(sqlist L);
void change(sqlist L, int l, int e);
bool empty(sqlist L);
void newm(sqlist& L);
#define MAXSIZE 100
int main()
{
sqlist L;
int l, e, p, temp;
printf("* 1、建立线性表(初始化) *\n");
printf("* 2、查找元素(定位) *\n");
printf("* 3、插入元素 *\n");
printf("* 4、删除元素 *\n");
printf("* 5、显示元素 *\n");
printf("* 6、修改元素(存储) *\n");
printf("* 7、判断是否为空表 *\n");
printf("* 8、计算数据表长度 *\n");
printf("* 9、销毁数据表 *\n");
printf("* 0、退出程序 *\n");
printf("-----------------------------------------------------------------\n");
printf("提示:请首先初试化线性表!\n");
while (1)
{
printf("请输入你的选项:0-9:\n");
scanf_s("%d", &temp);
if (temp == 0)
break;
switch (temp)
{
case 1:
{
newm(L);
break;
}
case 2:
{
printf(" input the elem to locate\n");
scanf_s("%d", &e);
p = locate(L, e);
if (!p)
{
printf("error\n");
}
else
{
printf("%d\n", p);
}
break;
}
case 3:
{
printf("please input the location and the data to Insert\n");
scanf_s("%d %d", &l, &e);
Insert(L, l, e);
print(L);
break;
}
case 4:
{
printf("input the location to delete:\n");
scanf_s("%d", &l);
deletem(L, l);
print(L);
break;
}
case 5:
{
printf("please input the location of the elem \n");
scanf_s("%d", &l);
Get(L, l, e);
printf("%d\n", e);
break;
}
case 6:
{
printf("please input the location and the elem to change\n");
scanf_s("%d %d", &l, &e);
change(L, l, e);
print(L);
break;
}
case 7:
{
if (empty(L))
{
printf("it is a sqlist length %d\n", L.length);
}
else
printf("it is empty\n");
break;
}
case 8:
{
printf("the length of the sqlist is %d\n", L.length);
break;
}
case 9:
{
free(L.i);
printf("it has been deleted\n");
break;
}
}
}
return 0;
}
bool newsqlist(sqlist& L)
{
L.i = (int*)(malloc)(sizeof(int[MAXSIZE]));
if (!L.i)
{
return false;
}
else
{
L.length = 0;
return true;
}
}
bool Insert(sqlist& L, int i, int j)
{
if (i<1 || i>L.length + 1)
{
return false;
}
if (L.length >= MAXSIZE)
return false;
for (int j = L.length - 1; j >= i - 1; j–)
{
L.i[j + 1] = L.i[j];
}
L.i[i - 1] = j;
L.length++;
return true;
}
bool Get(sqlist &L, int i, int& e)
{
if (i<1 || i>L.length + 1)
return false;
e = L.i[i - 1];
return true;
}
bool deletem(sqlist& L, int i)
{
if (i<1 || i>L.length + 1)
return false;
else
{
for (int j = i; j < L.length; j++)
{
L.i[j - 1] = L.i[j];
}
L.length–;
}
return true;
}
int locate(sqlist L, int i)
{
for (int j = 0; j < L.length; j++)
{
if (L.i[j] == i)
{
return j + 1;
}
}
return 0;
}
void print(sqlist L)
{
int e;
printf(“the sqliat is:”);
for (int i = 1; i <= L.length; i++)
{
Get(L, i, e);
printf("%d “, e);
}
printf(”\n");
return;
}
void change(sqlist L, int l, int e)
{
deletem(L, l);
Insert(L, l, e);
}
bool empty(sqlist L)
{
if (L.length == 0)
return 0;
return 1;
}
void newm(sqlist &L)
{
if (newsqlist(L))
{
int l, j;
printf(“new sqlist ready\n”);
printf(“please input the length:\n”);
scanf_s("%d", &l);
printf(“please input the data:\n”);
for (int i = 1; i <= l; i++)
{
scanf_s("%d", &j);
Insert(L, i, j);
}
print(L);
}
else
printf(“create sqlist error\n”);
}