郑仔102 2023-10-27 17:16 采纳率: 33.3%
浏览 10
已结题

用数据结构的c++代码完成

img


用c++语言完成 用数据结构顺序表的代码写 并且完成每一项要求 最好带注释

  • 写回答

15条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2023-10-27 17:16
    关注
    获得0.60元问题酬金

    引自强大而免费的微信小程序:皆我百晓生

    #include <iostream>
    using namespace std;
    
    const int MAX_SIZE = 100;
    
    struct SeqList {
        int data[MAX_SIZE];
        int length;
    };
    
    // 初始化顺序表
    void init(SeqList &L) {
        L.length = 0;
    }
    
    // 判断顺序表是否为空
    bool isEmpty(SeqList L) {
        return L.length == 0;
    }
    
    // 求顺序表的长度
    int getLength(SeqList L) {
        return L.length;
    }
    
    // 获取顺序表指定位置元素
    bool getElem(SeqList L, int index, int &element) {
        if (index < 1 || index > L.length) {
            return false;
        }
        element = L.data[index - 1];
        return true;
    }
    
    // 求指定元素的前驱
    bool getPreElem(SeqList L, int element, int &preElem) {
        for (int i = 1; i < L.length; i++) {
            if (L.data[i] == element) {
                preElem = L.data[i - 1];
                return true;
            }
        }
        return false;
    }
    
    // 求指定元素的后继
    bool getNextElem(SeqList L, int element, int &nextElem) {
        for (int i = 0; i < L.length - 1; i++) {
            if (L.data[i] == element) {
                nextElem = L.data[i + 1];
                return true;
            }
        }
        return false;
    }
    
    // 在顺序表指定位置插入元素
    bool insertElem(SeqList &L, int pos, int element) {
        if (pos < 1 || pos > L.length + 1 || L.length >= MAX_SIZE) {
            return false;
        }
        for (int i = L.length - 1; i >= pos; i--) {
            L.data[i + 1] = L.data[i];
        }
        L.data[pos - 1] = element;
        L.length++;
        return true;
    }
    
    // 删除顺序表指定位置元素
    bool deleteElem(SeqList &L, int pos, int &element) {
        if (pos < 1 || pos > L.length) {
            return false;
        }
        element = L.data[pos - 1];
        for (int i = pos; i < L.length; i++) {
            L.data[i - 1] = L.data[i];
        }
        L.length--;
        return true;
    }
    
    // 清空顺序表
    void clear(SeqList &L) {
        L.length = 0;
    }
    
    // 显示顺序表中的元素
    void display(SeqList L) {
        for (int i = 0; i < L.length; i++) {
            cout << L.data[i] << " ";
        }
        cout << endl;
    }
    
    int main() {
        SeqList list;
        int choice;
        int option;
        int position;
        int element;
        int preElem;
        int nextElem;
    
        init(list);
        
        while (true) {
            cout << "1----清空线性表" << endl;
            cout << "2----判断线性表是否为空" << endl;
            cout << "3----求线性表长度" << endl;
            cout << "4----获取线性表指定位置元素" << endl;
            cout << "5----求前驱" << endl;
            cout << "6----求后继" << endl;
            cout << "7----在线性表指定位置插入元素" << endl;
            cout << "8----删除线性表指定位置元素" << endl;
            cout << "9----显示线性表" << endl;
            cout << "退出,输入一个负数!" << endl;
            cout << "请输入操作代码:" << endl;
            cin >> choice;
    
            switch (choice) {
                case 1:
                    clear(list);
                    cout << "线性表已清空" << endl;
                    break;
                case 2:
                    if (isEmpty(list)) {
                        cout << "线性表为空" << endl;
                    } else {
                        cout << "线性表非空" << endl;
                    }
                    break;
                case 3:
                    cout << "线性表长度为:" << getLength(list) << endl;
                    break;
                case 4:
                    cout << "请输入要获取元素的位置:" << endl;
                    cin >> position;
                    if (getElem(list, position, element)) {
                        cout << "位置 " << position << " 的元素为:" << element << endl;
                    } else {
                        cout << "输入位置不合法" << endl;
                    }
                    break;
                case 5:
                    cout << "请输入要求前驱的元素:" << endl;
                    cin >> element;
                    if (getPreElem(list, element, preElem)) {
                        cout << "元素 " << element << " 的前驱为:" << preElem << endl;
                    } else {
                        cout << "不存在该元素的前驱" << endl;
                    }
                    break;
                case 6:
                    cout << "请输入要求后继的元素:" << endl;
                    cin >> element;
                    if (getNextElem(list, element, nextElem)) {
                        cout << "元素 " << element << " 的后继为:" << nextElem << endl;
                    } else {
                        cout << "不存在该元素的后继" << endl;
                    }
                    break;
                case 7:
                    cout << "请输入要插入的位置和元素,用空格隔开:" << endl;
                    cin >> position >> element;
                    if (insertElem(list, position, element)) {
                        cout << "插入成功" << endl;
                    } else {
                        cout << "插入失败,位置不合法或顺序表已满" << endl;
                    }
                    break;
                case 8:
                    cout << "请输入要删除的位置:" << endl;
                    cin >> position;
                    if (deleteElem(list, position, element)) {
                        cout << "删除成功,删除的元素为:" << element << endl;
                    } else {
                        cout << "删除失败,位置不合法" << endl;
                    }
                    break;
                case 9:
                    display(list);
                    break;
                default:
                    return 0;
            }
        }
    
        return 0;
    }
    
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 11月4日
  • 请回答用户的提问 10月27日
  • 创建了问题 10月27日