欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

数据结构之顺序表例程 简易电话薄

程序员文章站 2022-04-22 11:28:06
...

验证结果如下:

数据结构之顺序表例程 简易电话薄

实验代码如***释部分待完善。

#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;
    int Length;
    int ListLength;
} 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;
    L->ListLength = Max_Length;
    return OK;
}

Status ClearList (SeqList *L)
{
    L->Length=0;
    return OK;

}

Status EmptyList (SeqList *L)
{
    return (L->Length == 0);
}

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){
        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];
    L->data[i-1] = e;
    L->Length++;

    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];
    for (k = i; k< L->Length; k++)
      L->data[k-1] = L->data[k];
    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];

    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++;
    if(i<L->Length)
      return i+1;
    else
      return 0;
}

void main()
{
    ElemType e, *re;
    SeqList *List;
    int choice, i;
    List = (SeqList *)malloc(sizeof(SeqList));
    if (InitList(List)!=OK)
      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;
        }
    }
}