实验一:数据结构之顺序表例程 简易电话薄

验证结果如下:

实现代码如下:

#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;
        }
    }
}

 

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dingdongkk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值