验证结果如下:
实现代码如下:
#include
#include
#include
#include
//#include #define OK 0 //成功执行
#define Err_Memory -1 //内存分配错误
#define Err_InvalidParam -2 //输入参数无效
#define Err_Overflow -3 //溢出错误
#define Err_IllegalPos -4 //非法位置
#define Err_NoResult -5 //无法返回结果或返回结果为空
#define Max_Length 100 //顺序表最大长度
#define Increment_Length 10 //顺序表存储空间分配增量typedef struct {char name[11];char department[15];char phone[15];char mobile[18];
} ElemType; //定义顺序表的元素类型ElemTypetypedef struct {ElemType * data; //data指向存储数据元素的一维数组,初始大小为Max_Lengthint Length; //顺序表的实际长度,其值小于等于ListLengthint ListLength; //当前顺序表分配的空间大小,初始值为Max_Length
} SeqList; //顺序表结构定义typedef int Status; //定义返回状态Status InitList (SeqList *L)
{L->data = (ElemType *)malloc(Max_Length*sizeof(ElemType)); //分配内存空间if (!L->data) //如果内存分配失败,返回内存分配错误return Err_Memory;L->Length = 0; //顺序表的实际长度置为0L->ListLength = Max_Length; //当前顺序表存储大小置为Max_Lengthreturn OK; //成功返回
}Status ClearList (SeqList *L)
{L->Length=0; //顺序表的实际长度置为0return OK; //成功返回}Status EmptyList (SeqList *L)
{return (L->Length == 0); //如果Length为0,返回True,否则返回False
}Status LengthList (SeqList *L)
{return L->Length; //返回顺序表的长度
}/*Status TraverseList (SeqList *L)
{int i;for(i = 0; iLength; i++)printf("%d\t",L->data[i]);
}*/Status InsertList (SeqList *L, int i, ElemType e)
{int k;ElemType * newdata;if (i<1 || i> L->Length &#43; 1) //判断插入位置是否为无效位置return Err_IllegalPos;if (L->Length &#61;&#61; L->ListLength){//如果顺序表满&#xff0c;增加分配&#xff08;增量由Increment_Length指定&#xff09;newdata &#61; (ElemType *)realloc(L->data,(L->ListLength&#43;Increment_Length)*sizeof(ElemType));if(!newdata)return Err_Memory; //如果重新分配失败&#xff0c;返回内存分配错误L->data &#61; newdata; //新基址L->ListLength &#43;&#61; Increment_Length;//增加存储空间大小}for (k&#61;L->Length-1; k>i-1;k--)L->data[k&#43;1] &#61; L->data[k]; //将第i个位置及以后的元素后移一个位置L->data[i-1] &#61; e; //将元素e插入到第i个位置&#xff08;下标为i-1&#xff09;L->Length&#43;&#43;; //顺序表长度加1return OK;
}Status DeleteList (SeqList *L, int i, ElemType *e)
{int k;if (L->Length &#61;&#61; 0)return Err_InvalidParam; //顺序表为空if (i<1 || i>L->Length) //删除位置不合法return Err_IllegalPos;*e &#61; L->data[i-1]; //将第i个元素放到e中for (k &#61; i; kLength; k&#43;&#43;)L->data[k-1] &#61; L->data[k];//将第i&#43;1及其以后元素依次前移一位L->Length --; //表的长度减一return OK;
}//Status LocateList (SeqList *L, ElemType e);Status GetElem (SeqList *L, int i, ElemType *e)
{if (i<1 || i>L->Length)return Err_IllegalPos; //位置不合法*e &#61; L->data[i-1]; //将第i个位置的数据元素保存到ereturn OK;
}void TraverseList(SeqList *L) //遍历算法
{int i;for (i&#61;0;iLength;i&#43;&#43;)printf("%s\t%s\t%s\t%s\t\n",L->data[i].name, L->data[i].department,L->data[i].phone, L->data[i].mobile); //输出姓名&#xff0c;单位&#xff0c;固定电话和移动电话}int LocateList(ElemType e, SeqList *L)//定位算法
{int i &#61; 0;while(iLength&&strcmp(L->data[i].name, e.name)!&#61;0)//从第一个元素比较i&#43;&#43;; //L->data[i].name与e.name相等时 strcmp函数返回0if(iLength) //定位成功&#xff0c;返回e出现的位置return i&#43;1;elsereturn 0; //定位失败
}void main()
{ElemType e, *re;SeqList *List; //声明顺序表int choice, i;List &#61; (SeqList *)malloc(sizeof(SeqList)); //为顺序表List分配地址if (InitList(List)!&#61;OK) //初始化线性表Listreturn;while (1){ //循环操作printf("\n电话本管理\n");printf("1. 插入记录\t2. 查找记录\t3. 删除记录\t4. 浏览记录\t5. 退出\n");printf("\n请选择&#xff08;1-5&#xff09;\n");scanf("%d", &choice);switch(choice){case 1:printf("请输入姓名&#xff1a;");scanf("%s", e.name);printf("请输入单位&#xff1a;");scanf("%s", e.department);printf("请输入固定电话&#xff1a;");scanf("%s", e.phone);printf("请输入移动电话&#xff1a;");scanf("%s", e.mobile);if(InsertList(List, List->Length&#43;1, e) &#61;&#61; OK)printf("\n插入记录成功\n");break;case 2:printf("请输入要查找的姓名&#xff1a;");scanf("%s", e.name);i&#61;LocateList(e, List);if (i>0){re&#61;(ElemType *)malloc(sizeof(ElemType));GetElem(List, i, re);printf("\n详细信息如下&#xff1a;\n\n");printf("姓名&#xff1a;%s\t 单位&#xff1a;%s\t 固定电话: %s\t 移动电话&#xff1a;%s\n",re->name, re->department, re->phone, re->mobile);} else {printf("查无此人");}break;case 3:printf("请输入要删除信息的姓名&#xff1a;");scanf("%s", e.name);i&#61;LocateList(e, List);if (i>0){re&#61;(ElemType *)malloc(sizeof(ElemType));if (DeleteList(List, i, re) &#61;&#61; OK)printf("删除成功\n");elseprintf("删除失败\n");} else {printf("查无此人");}break;case 4:printf("当前共有电话本记录%d条&#xff0c;以下是详细信息&#xff1a;\n\n", List->Length);printf("姓名\t 单位\t 固定电话\t 移动电话\n");TraverseList(List);break;case 5:break;default:printf("选择错误&#xff0c;请重新选择&#xff01;\n");break;}}
}