// 修改了多处
/*顺序表的基本操作*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 3
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR -1
#define OVERFLOW -2
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 == NULL)
return(OVERFLOW);
L.length = 0;
L.listsize = LIST_INIT_SIZE;
return OK;
}
void Destroylist(SqList &L) /*销毁顺序表*/
{
free(L.elem);
L.elem = NULL;
L.length = L.listsize = 0;
}
void Clearlist_Sq(SqList &L)
/* 清空顺序表, 元素置0 */
{
int i;
for(i = 0; i < L.length; i++)
L.elem[i] = 0;
}
Status Listempty_Sq(SqList &L) /*测试顺序表是否为空*/
{
if(L.length == 0)
return TRUE;
else
return FALSE;
}
Status ListInsert_Sq(SqList &L, int i, ElemType e) /*在第i个位置上插入一个元素*/
{
ElemType *p;
int j;
if (i < 1 || i > L.length + 1)
return ERROR;
if (L.length + 1 > L.listsize) {
p = (ElemType*)realloc(L.elem,(L.length + LISTINCREMENT)*sizeof(ElemType));
if (p == NULL)
exit(OVERFLOW);
L.elem = p;
L.listsize += LISTINCREMENT;
}
for (j = L.length; j >= i; --j )
L.elem[j] = L.elem[j-1];
L.elem[j]=e ;
++L.length ;
return OK;
}
Status ListDelete_Sq(SqList &L, int i, int &e) /*删除第i个位置上的元素*/
{
if(i < 1 || i > L.length)
return ERROR;
e = L.elem[i - 1];
for(i; i < L.length; i++)
L.elem[i - 1] = L.elem[i];
--L.length;
return OK;
}
int LocateElem_Sq(SqList &L, ElemType e) /*返回元素e在顺序表中的位置*/
{
int i = 0;
while(i < L.length) {
if(e == L.elem[i++])
return i;
}
return ERROR;
}
void Print_Sq(SqList L) /*输出顺序表*/
{
int i;
if (Listempty_Sq(L))
printf("\nSequential List's length is %d. It's empty!", L.length);
else
printf("\nSequential List's length is %d. These element are : ", L.length);
for(i = 0; i < L.length; i++)
printf(" %d", L.elem[i]);
}
void menu()
{
printf("\n");
printf("\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf("
1 ------- Print the Sequential List.\n");
printf("
2 ------- Insert a data in the Sequential List.\n");
printf("
3 ------- Delete a data in the Sequential List.\n");
printf("
4 ------- Get a element's locationt in the SqList.\n");
printf("
5 ------- Clear the Sequential List.\n");
printf("
6 ------- Exit.\n");
printf("* * * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf("\n");
}
void menuselect(SqList L)
{
int k, i;
ElemType e;
do
{
menu();
printf("Please choose: ");
scanf("%d", &k);
while((i = getchar()) != '\n' && i != EOF)
continue;
switch(k)
{
case 1:
Print_Sq(L);
break;
case 2:
printf("\nInput the location and data you want to Insert: ");
scanf("%d,%d", &i, &e);
if (ListInsert_Sq(L,i,e) == ERROR)
printf("Insert location is not correct!\n");
break;
case 3:
printf("\nInput the location you want to delete data: ");
scanf("%d", &i);
if (ListDelete_Sq(L,i,e) == ERROR)
printf("Delete location is not exit!\n");
else
printf("\nDelete data is %d.", e);
break;
case 4:
printf("\nInput the data you want to find: ");
scanf("%d", &e);
if (LocateElem_Sq(L,e) != ERROR)
printf("\nData %d location in the Sequential List is %d.", e, LocateElem_Sq(L,e));
else
printf("\nData %d is not in the Sequential List.\n", e);
break;
case 5:
Clearlist_Sq(L);
break;
case 6:
return;
break;
default :
printf("Enter Error!\n");
break;
}
}while(1);
}
int main(void)
{
ElemType e;
SqList La;
int n, i;
clrscr();
Initlist_Sq(La);
printf("Input a number of the element in the Sequential List (n<=%d):", LIST_INIT_SIZE);
scanf("%d", &n);
printf("Enter these elements:");
for(i = 1; i <= n;i++)
{
scanf("%d", &e);
ListInsert_Sq(La, i, e);
}
menuselect(La);
getchar();
return 0;
}