zoukankan      html  css  js  c++  java
  • 顺序表-C语言实现

    顺序存储线性表的结构体:

    #define MAXSIZE 100        //数组最大长度
    typedef int ElemType;        //元素类型
    
    typedef struct            //定义线性表结构体
    {
        ElemType date[MAXSIZE];        //线性表存储元素的数组
        int length;                    //记录线性表的长度
    }sqList;                        //线性表的名称

    顺序存储的插入函数:

    /*
    线性表的插入函数
    *p  指向线性表的指针
    i   需要插入的位置
    e   将要插入的元素
    Status未返回值类型,在头文件处定义,格式为:typedef int Status;        //返回值类型,操作成功返回1(OK),失败返回0(ERROR)
    */ Status ListInsert(sqList *p, int i, ElemType e) { if(p->length == MAXSIZE) //如果表长度已经等于最大值,则表已满,返回ERROR return ERROR; if(i < 1 || i > p->length + 1) //如果i<1或i>表长+1,则位置有误,返回ERROR return ERROR; if(i <= p->length) //判断i是不是表尾位置 { for(int k = p->length; k >= i - 1; k--) //将表中i之后的元素全部后移一个位置 p->date[k+1] = p->date[k]; } p->date[i-1] = e; //将第i个位置的元素赋值为e p->length++; //将表长+1 return OK; //操作成功,返回OK }

    顺序存储的删除函数:

    /*
    线性表的删除操作函数
    *p  指向线性表的指针
    i   需要删除的位置
    *e  将删除的元素存入*e中返回
    */
    Status ListDel(sqList *p, int i, ElemType *e)
    {
        if(p->length == 0)        //判断线性表是否为空
            return ERROR;
        if(i < 1 || i > p->length)        //判断i的位置是否合理
            return ERROR;
        *e = p->date[i-1];        //将表中第i个元素的值赋值给*e
        printf("删除的元素为 %d
    ", *e);
        if(i <= p->length)        //判断i是不是表尾元素
        {
            for(int k = i-1; k <= p->length; k++)    //将表中i后的元素全部向前移一位
                p->date[k] = p->date[k+1];
        }
        p->length--;        //将表长-1
        return OK;            //操作成功返回OK
    }

    顺序存储的索引查找函数:

    /*
    线性表的查询操作
    p  表的形参
    i  需要得到的位置
    *e 将所查位置的元素赋值给*e返回
    */
    Status GetElem(sqList p, int i, ElemType *e)
    {
        if(p.length == 0 || i < 1 || i > p.length)        //判断表是否为空,i位置是否合理
            return ERROR;
        *e = p.date[i-1];        //将表的第i个元素赋值给*e
        printf("此处的元素为 %d
    ", *e);
        return OK;                //操作成功返回OK
    }

    顺序存储的修改函数:

    /*
    修改表中i位置的元素内容
    */
    Status UpdateList(sqList *p, int i, ElemType e)
    {
        if(p->length == 0 || i < 1 || i > p->length)
            return ERROR;
        p->date[i-1] = e;
        return OK;
    }

    顺序存储的遍历打印函数:

    /*
    遍历打印表中元素
    */
    Status PrintList(sqList p)
    {
        int i = 0;
        if(p.length == 0)
            return ERROR;
        while(i < p.length)
        {
            printf("%d ", p.date[i]);
            i++;
        }
        printf("
    ");
        return OK;
    }

    主函数:

    void main()
    {
        sqList list;        //声明线性表的变量
        list.length = 0;    //设置表长为0,相当于初始化
        int i, e;            //i为元素位置,e为元素内容
    
        while(true)
        {
            printf("请选择对线性表的操作:
    ");
            printf("1.插入
    ");
            printf("2.删除
    ");
            printf("3.查找
    ");
            printf("4.输出
    ");
            printf("5.修改
    ");
            printf("6.退出
    ");
            int a;
            scanf("%d", &a);
            switch(a)
            {
                case 1:
                    printf("请输入需要插入的位置:");
                    scanf("%d", &i);
                    printf("请输入需要插入的元素:");
                    scanf("%d", &e);
                    if(ListInsert(&list, i, e))
                        printf("插入成功
    ");
                    else
                        printf("插入失败
    ");
                    break;
                case 2:
                    printf("请输入需要删除的位置:");
                    scanf("%d", &i);
                    if(ListDel(&list, i, &e))
                        printf("删除成功
    ");
                    else
                        printf("删除失败
    ");
                    break;
                case 3:
                    printf("请输入需要查找的位置:");
                    scanf("%d", &i);
                    if(GetElem(list, i, &e))
                        printf("查询成功
    ");
                    else
                        printf("查询失败
    ");
                    break;
                case 4:
                    PrintList(list);
                    break;
                case 5:
                    printf("请输入需要修改的位置:");
                    scanf("%d", &i);
                    printf("请输入新的的元素:");
                    scanf("%d", &e);
                    if(UpdateList(&list, i, e))
                        printf("修改成功
    ");
                    else
                        printf("修改失败
    ");
                    break;
                case 6:
                    return;
                default:
                    printf("选择错误
    ");
                    break;
            }
        }
    }

    经检测,所有代码均可执行!

  • 相关阅读:
    Delphi ini文件结构简介
    Delphi 格式化函数 Format函数
    Delphi 获取系统的语言环境参数GetSystemDefaultLangID、VerLanguageName、GetLocaleInfo
    Delphi DBGrid 实现复选框
    SQL 循环语句几种写法
    Delphi 左键代替右键
    CSV (逗号分隔值文件格式)
    中国人工智能AI框架自主研发
    电动汽车:不是如果,而是什么时候和多快
    KITTI数据集上MaskRCNN检测效果示例
  • 原文地址:https://www.cnblogs.com/yurui/p/9498767.html
Copyright © 2011-2022 走看看