/*
*功能:新建,销毁,清空,判空,获取长度,获取元素,定位,前趋,后趋,插入,删除,遍历
*/
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct{
ElemType *elem;
int length;
int listsize;
}SqList;
Status InitList_Sq(SqList *L);
Status DestroyList_Sq(SqList *L);
Status ClearList_Sq(SqList *L);
Status ListEmpty_Sq(SqList L);
Status ListLength_Sq(SqList L);
Status GetElem_Sq(SqList L,int i,ElemType *e);
Status LocateElem_Sq(SqList L,ElemType e,int (*compare)(ElemType a,ElemType b));
Status PriorElem_Sq(SqList L,ElemType cur_e,ElemType *pre_e);
Status NextElem_Sq(SqList L,ElemType cur_e,ElemType *next_e);
Status ListInsert_Sq(SqList *L,int i,ElemType e);
Status ListDelete_Sq(SqList *L,int i,ElemType *e);
Status ListTraverse_Sq(SqList L,int (*visit)(ElemType L,ElemType i));
Status equal(ElemType a,ElemType b);
Status biger(ElemType a,ElemType b);
Status smaller(ElemType a,ElemType b);
Status visit(ElemType L,ElemType i);
#include <stdio.h>
#include <stdlib.h>
void menu() /*菜单函数*/
{
printf("\n");
printf("\n ==============================================================");
printf("\n | |");
printf("\n | 菜单....... |");
printf("\n | a. 新建空表; |");
printf("\n | b. 销毁线性表; |");
printf("\n | c. 清空线性表; |");
printf("\n | d. 判空; |");
printf("\n | e. 获取表长; |");
printf("\n | f. 获取表中特定位置的元素的值; |");
printf("\n | g. 获取某值在表的位置; |");
printf("\n | h. 得到前驱; |");
printf("\n | i. 获取后继; |");
printf("\n | j. 插入元素; |");
printf("\n | k. 删除元素; |");
printf("\n | l. 遍历整个线性表; |");
printf("\n | q. 退出. |");
printf("\n ==============================================================");
}
SqList choose(SqList L,char x)
{ int data,t,e,m=0,n,position=0;
if(x<'a'||x>'q')
printf("\n你输入的数据不正确,请选择正确的字母输入!\n");/*若输入的不是选项 提示出错*/
else
switch(x)
{
case 'a': InitList_Sq(&L); break;/*新建并且初始化线性表 若成功显示成功*/
case 'b': DestroyList_Sq(&L);break; /*销毁线性表*/
case 'c': ClearList_Sq(&L);break; /*清空线性表*/
case 'd': if(ListEmpty_Sq(L)) printf("这个表是空的.\n");/*输出线性表为空*/
else printf("这个表不是空的.\n");break;
case 'e': printf("线性表的长度是 %d.\n",ListLength_Sq(L));break;/*获取表长*/
case 'f': printf("请输入结点位置:");
scanf("%d",&position);getchar();/*输入要获取内容的结点位置*/
if(GetElem_Sq(L,position,&data))
printf("该结点位置的元素值是 %d.\n",data);/*若结点位置存在 则输出数据*/
break;
case 'g': printf("请输入你要查找的元素的值: ");
scanf("%d",&data);getchar();
position = LocateElem_Sq(L,data,&equal);
if(0 == position) printf("该值的元素在表中不存在\n");
else printf("该值的元素在表中位置是 %d.\n",position);
break;
case 'h': printf("请输入元素值: ");
scanf("%d",&data);getchar();/*输入要查找的数据内容*/
if(PriorElem_Sq(L,data,&e))
printf("该元素的前驱是 %d.\n",e);/*若前驱存在 则输出*/
break;
case 'i': printf("请输入元素值: ");
scanf("%d",&data);getchar();/*输入要查找的数据内容*/
if(NextElem_Sq(L,data,&e))
printf("该元素的前驱是 %d.\n",e);/*若后继存在 则输出*/
break;
case 'j': printf("你需要插入多少个元素?\n");
scanf("%d",&n);getchar();
printf("请输入插入的结点位置和元素值:");
for(m=0;m<n;m++)
{ scanf("%d %d",&position,&data);getchar();
ListInsert_Sq(&L,position,data); //插入数据
printf("插入的位置在 %d 元素值是 %d \n.",position,data);
}
break;
case 'k': printf("请输入你想删除的结点的位置:");
scanf("%d",&position);getchar();/*输入要删除的结点位置*/
if(ListDelete_Sq(&L,position,&data))
printf("你删除的结点位置的元素值为 %d.\n",data);/*若线性表不为空 则输出所删结点的数据*/
break;
break;
case 'l': ListTraverse_Sq(L,&visit);break;/*输出线性表中所有的数据*/
case 'q': DestroyList_Sq(&L); exit(1); /*销毁线性表 退出*/
}
return L;
}
int main()
{
SqList L;
char a=0;
menu();/*显示菜单*/
do
{
printf("\n请选择你需要的操作,回车执行: ");
a=getchar(); /*输入字符用于判断是否继续*/
getchar(); /*吸收回车符*/
L = choose(L,a);
}while(1);/*若输入为Y或y 则循环以继续否则退出*/
return 0;
}
Status InitList_Sq(SqList *L)
{ //构建一个空的顺序线性表L
L->elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L->elem){printf("新建失败!!\n");exit(OVERFLOW);}
L->length = 0;
L->listsize = LIST_INIT_SIZE;printf("新建成功!!\n");
return OK;
}//InitList_Sq
Status DestroyList_Sq(SqList *L)
{ //销毁顺序线性表L
if(!L->elem)exit(ERROR);
free(L->elem);
printf("线性表销毁成功!!\n");
return OK;
}//DestroyList_Sq
Status ClearList_Sq(SqList *L)
{ //重置顺序线性表L为空表
if(!L->elem){printf("线性表不存在!\n");exit(ERROR);}
L->length = 0; printf("清除成功!!\n");
return OK;
}//ClearList_Sq
Status ListEmpty_Sq(SqList L)
{ //判断顺序线性表L是否为空
if(!L.elem){printf("线性表不存在!\n");exit(ERROR);}
if(L.length == 0)
return TRUE;
else if(L.length != 0)
return FALSE;
}//ListEmpty_Sq
Status ListLength_Sq(SqList L)
{ //顺序线性表L的元素个数
if(!L.elem){printf("线性表不存在!!\n"); return ERROR;}
return L.length;
}//ListLength_Sq
Status GetElem_Sq(SqList L,int i,ElemType *e)
{ //返回顺序线性表L中第i个元素的值
if(!L.elem){printf("线性表不存在!!\n"); return ERROR;}
if(i < 1 || i > L.length){printf("输入的位置不在线性表中!!\n");return ERROR;}
*e = L.elem[i-1];
return OK;
}//GetElem_Sq
Status LocateElem_Sq(SqList L,ElemType e,int (*compare)(ElemType,ElemType))
{ //返回顺序线性表L中第1个满足compare()的数据元素的位序
if(!L.elem){printf("线性表不存在!!\n"); return ERROR;}
int i=1;
ElemType *p = L.elem;
while(i <= L.length && !(*compare)(*p++,e)) ++i;
if(i <= L.length) return i;
else return 0;
}//LocateElem_Sq
Status PriorElem_Sq(SqList L,ElemType cur_e,ElemType *pre_e)
{ //若cur_e是L的数据元素,且不是第一个,则用pre_e返回它的前驱
if(!L.elem){printf("线性表不存在!!\n"); return ERROR;}
ElemType *p = L.elem + 1;
int i = 2;
while (i < L.length && *p != cur_e)
{
i++;
p++;
}
if (i == L.length)
return ERROR;
else
{
*pre_e = *--p;
return OK;
}
}//PriorElem_Sq
Status NextElem_Sq(SqList L,ElemType cur_e,ElemType *next_e)
{ //若cur_e是L的数据元素,且不是最后一个,则用next_e返回它的后继
if(!L.elem){printf("线性表不存在!!\n"); return ERROR;}
ElemType *p = L.elem;
int i = 1;
while (i < L.length && *p != cur_e)
{
i++;
p++;
}
if (i == L.length)
return ERROR;
else
{
*next_e = *++p;
return OK;
}
}//NextElem_Sq
Status ListInsert_Sq(SqList *L,int i,ElemType e)
{ //在顺序线性表L中第i个位置插入新元素e,L的长度加1
if(!L->elem){printf("线性表不存在!!\n"); return ERROR;}
if(i < 1 || i > L->length+1){printf("位置不在线性表中!!\n");return ERROR;}
if(L->length >= L->listsize)
{
ElemType newbase = (ElemType *)realloc(L->elem,
(L->listsize+LISTINCREMENT)*sizeof(ElemType));
if(!newbase)exit(ERROR);
L->elem = newbase;
L->listsize += LISTINCREMENT;
}
ElemType *p,*q;
q = &(L->elem[i-1]);
for(p = &L->elem[L->length-1];p >= q;--p) *(p+1) = *p;
*q = e;
++L->length;
return OK;
}//ListInsert_Sq
Status ListDelete_Sq(SqList *L,int i,ElemType *e)
{ //在顺序线性表L中删除第i个元素,并用e返回其值
if(!L->elem){printf("线性表不存在!!\n"); return ERROR;}
if(i < 1 || i > L->length+1){printf("输入的位置不在线性表中!!\n");return ERROR;}
ElemType *p = &(L->elem[i-1]);
*e = *p;
ElemType *q = L->elem+L->length-1;
for(++p;p <= q;++p) *(p-1) = *p;
--L->length;
return OK;
}//ListDelete_Sq
Status ListTraverse_Sq(SqList L,int (*visit)(ElemType,ElemType))
{ //依次对L中的每个数据元素调用函数visit()。一旦visit()失败,则操作失败。
//假设visit()的功能是判断数据元素的值是否大于100,大于100返回FALSE,否则返回TRUE
if(!L.elem){printf("线性表不存在!!\n"); return ERROR;}
ElemType *p = L.elem;
int i=1;
while (i <= L.length && (*visit)(*p++,i))
{
printf("表中第 %d 个元素是: %d /n",i,L.elem[i-1]);
++i;
//j = (*visit)(*p++,i);
}
return OK;
}//ListTraverse_Sq
Status equal(ElemType a,ElemType b)
{
if(a == b)return 1;
else return 0;
}//equal
Status biger(ElemType a,ElemType b)
{
if(a >= b)return 1;
else return 0;
}//equal
Status smaller(ElemType a,ElemType b)
{
if(a <= b)return 1;
else return 0;
}//smaller
Status visit(ElemType L,ElemType i)
{
if(100 >= L)
{
return TRUE;
}
else
{
return FALSE;
}
}