
#include <stdio.h>
#include <stdlib.h> //修改
#include <string.h>
#include <math.h>
typedef int ElemType;
#define MaxSize 60
#define FALSE 0 //修改
#define TRUE 1 //修改
typedef struct {
ElemType data[MaxSize];
int length;
}Sqlist;
void Initlist_Sq(Sqlist*& L)
{
L = (Sqlist*)malloc(sizeof(Sqlist));
L->length = 0;
}
void Creatlist_Sq(Sqlist*& L, char a[], int n) // 中文 ,逗号
{
int i;
//L = (Sqlist*)malloc(sizeof(Sqlist)); 修改
for (i = 0; i < n; i++)
/*scanf_s("d", &a[i]);*/
L->data[i] = a[i];
L->length = n;
}
bool ListInsert_Sq(Sqlist*& L, int i, ElemType e)//插入
{
int j;
char f;
if (i < 1 || i > L->length + 1)
return FALSE;
i--;
for (j = L->length; j > i; j--)
L->data[j] = L->data[j - 1]; //L->data[j] = L->data[i]; 修改
L->data[i] = e; //L->data[i - 1] = e; 修改
L->length++;
return TRUE;
}
void ListTraverse_Sq(Sqlist*& L)//输出
{
int i;
for (i = 0; i < L->length; i++)
printf("%5c", L->data[i]);
printf("\n");
}
int ListLength_Sq(Sqlist*& L)
{
return (L->length);
}
bool ListEmpty_Sq(Sqlist*& L)
{
return (L->length == 0);
}
//void ListTravers_Sq(Sqlist*& L)
//{
// int i = 2;
// printf("%d", L->data[i]);
// printf("\n");
//}
bool ListDelete_Sq(Sqlist*& L, int i, ElemType& e)
{
int j;
if ((i < 1) || (i > L->length))
return FALSE;
i--;
e = L->data[i];
for (j = i; j < L->length - 1; j++)
L->data[j] = L->data[j + 1];
L->length--;
return TRUE;
}
void DestroyList_Sq(Sqlist*& L)
{
free(L);
}
//菜单
void menu()
{
printf("********1.初始化创建顺序表 2.插入*********\n");
printf("********3.输出L 4.输出长度*********\n");
printf("********5.判断是否为空 6.删除***\n");
printf("********7.清空 8.输出元素a的逻辑位置*********\n");
}
int main()
{
Sqlist* L; //Sqlist L; 修改
int choice, i = 7;
char f;//修改
char a[] = { 'a','b','c','d','e' };
//InitList(L); 修改
while (1)
{
menu();
printf("请输入菜单序号:\n");
scanf_s("%d", &choice);
if (9 == choice) break;
switch (choice)
{
case 1:
{Initlist_Sq(L); //初始化 修改
Creatlist_Sq(L, a, i); //创建顺序表 修改
break;
}
case 2:
{char ch = 'f';
i = 4;
ListInsert_Sq(L, i, ch); //插入位置 i =4 ,插入f
break;
}
case 3: {ListTraverse_Sq(L); //修改
break; }
case 4: {printf("顺序表L的长度为:%d\n", ListLength_Sq(L)); //修改
break; }
case 5: {if (ListEmpty_Sq(L))
printf("顺序表L为空!\n");
else
printf("顺序表L不为空!\n");
break; }
case 6: {char ah;
ListDelete_Sq(L,i,ah);
break; }
// ListTravers_Sq(Sqlist * &L);
case 7:
{DestroyList_Sq(L); break; }
default:printf("输入错误!!!\n");
}
}
return 0;
}