题目:编写26个字母按特定字母值插入或删除的完整程序
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef char 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 GetElem_Sq(SqList L,int i,ElemType *e);
Status ListInsert_Sq(SqList *L,int i,ElemType e);
Status ListTraverse_Sq(SqList L,int (*visit)(ElemType L,ElemType i));
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("操作:'1' :新建线性表\n");
printf(" '2' :销毁线性表\n");
printf(" '3' :退出操作\n");
printf("########################################################\n");
}
int main()
{
SqList L;
char a=0;
int position,i;
ElemType *e0,*e1,*e2,*e3;
menu();//操纵介绍
do
{
printf("请输入'a'~'z'或'A'~'Z'中一个插入到线性表L中或命令(Enter确定): ");
a=getchar(); //输入字符用于判断是否继续
getchar(); //吸收回车符
if('1' == a) InitList_Sq(&L);//创建L
else if('2' == a) DestroyList_Sq(&L);//销毁L
else if('3' == a) {DestroyList_Sq(&L);exit(0);} //退出操作
else if(('a' <= a && a <= 'z')||('A' <= a && a <= 'Z')){ //输入元素值并插入到线性表的正确位置
if(0 != L.length)
{ //当表不为空是的插入方式
GetElem_Sq(L,1,&e0);GetElem_Sq(L,L.length,&e1);
if(smaller(a,e0))position=1; //插入到表头
else if(smaller(e1,a))position=L.length+1; //插入到表尾
else
{ //插入到表中的算法
for(i=L.length-1;i>0;i--){
GetElem_Sq(L,i,&e2);
GetElem_Sq(L,i+1,&e3);
if(smaller(e2,a) && smaller(a,e3))position = i+1; //把元素插入到两个元素中间,前一个元素小于或等于它,后一元素大于或等于它
}}}
else position = 1; //当表为空时直接把值插入到表头
ListInsert_Sq(&L,position,a);
ListTraverse_Sq(L,&visit);
}
else printf("请输入正确的命令\n");
}while(a != 'EOF');//若输入为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 GetElem_Sq(SqList L,int i,ElemType *e)
{ //返回顺序线性表L中第i个元素的值
if(!L.elem){printf("线性表不存在!!\n"); return ERROR;}
if(i < 1 || i > L.length+1){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 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 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 个元素是: %c \n",i,L.elem[i-1]);
++i;
//j = (*visit)(*p++,i);
}
return OK;
}//ListTraverse_Sq
Status smaller(ElemType a,ElemType b)
{
if(a <= b)return 1;
else return 0;
}//smaller
Status visit(ElemType L,ElemType i)
{
if(256 >= L)
{
return TRUE;
}
else
{
return FALSE;
}
}