驗證結果如下:
實現代碼如下:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
//#include <sequence_table.h>
#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; //定義順序表的元素類型ElemType
typedef struct {
ElemType * data; //data指向存儲數據元素的一維數組,初始大小爲Max_Length
int Length; //順序表的實際長度,其值小於等於ListLength
int 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; //順序表的實際長度置爲0
L->ListLength = Max_Length; //當前順序表存儲大小置爲Max_Length
return OK; //成功返回
}
Status ClearList (SeqList *L)
{
L->Length=0; //順序表的實際長度置爲0
return 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; i< L->Length; 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 + 1) //判斷插入位置是否爲無效位置
return Err_IllegalPos;
if (L->Length == L->ListLength){//如果順序表滿,增加分配(增量由Increment_Length指定)
newdata = (ElemType *)realloc(L->data,
(L->ListLength+Increment_Length)*sizeof(ElemType));
if(!newdata)
return Err_Memory; //如果重新分配失敗,返回內存分配錯誤
L->data = newdata; //新基址
L->ListLength += Increment_Length;//增加存儲空間大小
}
for (k=L->Length-1; k>i-1;k--)
L->data[k+1] = L->data[k]; //將第i個位置及以後的元素後移一個位置
L->data[i-1] = e; //將元素e插入到第i個位置(下標爲i-1)
L->Length++; //順序表長度加1
return OK;
}
Status DeleteList (SeqList *L, int i, ElemType *e)
{
int k;
if (L->Length == 0)
return Err_InvalidParam; //順序表爲空
if (i<1 || i>L->Length) //刪除位置不合法
return Err_IllegalPos;
*e = L->data[i-1]; //將第i個元素放到e中
for (k = i; k< L->Length; k++)
L->data[k-1] = L->data[k];//將第i+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 = L->data[i-1]; //將第i個位置的數據元素保存到e
return OK;
}
void TraverseList(SeqList *L) //遍歷算法
{
int i;
for (i=0;i<L->Length;i++)
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); //輸出姓名,單位,固定電話和移動電話
}
int LocateList(ElemType e, SeqList *L)//定位算法
{
int i = 0;
while(i<L->Length&&strcmp(L->data[i].name, e.name)!=0)//從第一個元素比較
i++; //L->data[i].name與e.name相等時 strcmp函數返回0
if(i<L->Length) //定位成功,返回e出現的位置
return i+1;
else
return 0; //定位失敗
}
void main()
{
ElemType e, *re;
SeqList *List; //聲明順序表
int choice, i;
List = (SeqList *)malloc(sizeof(SeqList)); //爲順序表List分配地址
if (InitList(List)!=OK) //初始化線性表List
return;
while (1){ //循環操作
printf("\n電話本管理\n");
printf("1. 插入記錄\t2. 查找記錄\t3. 刪除記錄\t4. 瀏覽記錄\t5. 退出\n");
printf("\n請選擇(1-5)\n");
scanf("%d", &choice);
switch(choice){
case 1:
printf("請輸入姓名:");
scanf("%s", e.name);
printf("請輸入單位:");
scanf("%s", e.department);
printf("請輸入固定電話:");
scanf("%s", e.phone);
printf("請輸入移動電話:");
scanf("%s", e.mobile);
if(InsertList(List, List->Length+1, e) == OK)
printf("\n插入記錄成功\n");
break;
case 2:
printf("請輸入要查找的姓名:");
scanf("%s", e.name);
i=LocateList(e, List);
if (i>0){
re=(ElemType *)malloc(sizeof(ElemType));
GetElem(List, i, re);
printf("\n詳細信息如下:\n\n");
printf("姓名:%s\t 單位:%s\t 固定電話: %s\t 移動電話:%s\n",
re->name, re->department, re->phone, re->mobile);
} else {
printf("查無此人");
}
break;
case 3:
printf("請輸入要刪除信息的姓名:");
scanf("%s", e.name);
i=LocateList(e, List);
if (i>0){
re=(ElemType *)malloc(sizeof(ElemType));
if (DeleteList(List, i, re) == OK)
printf("刪除成功\n");
else
printf("刪除失敗\n");
} else {
printf("查無此人");
}
break;
case 4:
printf("當前共有電話本記錄%d條,以下是詳細信息:\n\n", List->Length);
printf("姓名\t 單位\t 固定電話\t 移動電話\n");
TraverseList(List);
break;
case 5:
break;
default:
printf("選擇錯誤,請重新選擇!\n");
break;
}
}
}