成都工业学院数据结构与算法(DSA)实验二:单链表

写在前面

1、基于2021级计算机类实验指导书

2、代码仅提供参考

3、如果代码不满足你的要求,请寻求其他的途径

运行环境

window11家庭版

CLion 2023.2.2

实验要求、源代码和运行结果

1.建立一个单链表,随机产生10个100以内的整数,并按要求完成:

(1)在屏幕上显示单链表中的10个整数;

(2)删除值为a的结点,若不存在a,则把a插入到表尾,显示更新后的单链表;

#include <iostream>
#include <ctime>

using namespace std;

// 单链表结点类
class Node {
public:
    int data;     // 存储整数值
    Node* next;   // 指向下一个结点的指针

    // 构造函数
    Node(int d) {
        data = d;
        next = nullptr;
    }
};

// 单链表类
class LinkedList {
private:
    Node* head;   // 指向链表头结点的指针

public:
    // 构造函数
    LinkedList() {
        head = nullptr;
    }

    // 插入结点到链表尾部
    void insert(int value) {
        // 创建新结点
        Node* newNode = new Node(value);

        // 如果链表为空,则将新结点设为头结点
        if (head == nullptr) {
            head = newNode;
        } else {
            // 遍历链表,找到最后一个结点
            Node* current = head;
            while (current->next != nullptr) {
                current = current->next;
            }

            // 插入新结点到最后
            current->next = newNode;
        }
    }

    // 删除值为value的结点
    void remove(int value) {
        // 链表为空
        if (head == nullptr) {
            return;
        }

        // 头结点的值为value
        if (head->data == value) {
            Node* temp = head;
            head = head->next;
            delete temp;
            return;
        }

        // 遍历链表,找到要删除结点的前一个结点
        Node* current = head;
        while (current->next != nullptr && current->next->data != value) {
            current = current->next;
        }

        // 找到要删除的结点或遍历完整个链表
        if (current->next != nullptr) {
            Node* temp = current->next;
            current->next = current->next->next;
            delete temp;
        } else {
            // 如果没有找到值为value的结点,则将value插入到表尾
            insert(value);
        }
    }

    // 显示链表中的元素
    void display() {
        if (head == nullptr) {
            cout << "链表为空" << endl;
        } else {
            Node* current = head;
            while (current != nullptr) {
                cout << current->data << " ";
                current = current->next;
            }
            cout << endl;
        }
    }
};

int main() {
    // 设置随机种子
    srand(time(nullptr));

    LinkedList list;

    // 生成10个100以内的随机整数并插入链表
    for (int i = 0; i < 10; i++) {
        int randNum = rand() % 100 + 1;  // 生成1至100的随机整数
        list.insert(randNum);
    }

    cout << "单链表中的整数:"<<endl;
    list.display();

    int a;
    cout << "请输入要删除的整数值:"<<endl;
    cin >> a;

    list.remove(a);

    cout << "更新后的单链表:"<<endl;
    list.display();

    return 0;
}

2.设计一个程序,生成两个按值非递减有序排列的线性表LA和LB,再将LA和LB归并为一个新的线性表LC,且LC中的数据仍按值非递减有序排列,输出线性表LA、LB、LC。

#include <iostream>

using namespace std;

// 单链表结点类
class Node {
public:
    int data;     // 存储整数值
    Node* next;   // 指向下一个结点的指针

    // 构造函数
    Node(int d) {
        data = d;
        next = nullptr;
    }
};

// 单链表类
class LinkedList {
private:
    Node* head;   // 指向链表头结点的指针

public:
    // 构造函数
    LinkedList() {
        head = nullptr;
    }

    // 插入结点到链表尾部
    void insert(int value) {
        // 创建新结点
        Node* newNode = new Node(value);

        // 如果链表为空,则将新结点设为头结点
        if (head == nullptr) {
            head = newNode;
        } else {
            // 遍历链表,找到最后一个结点
            Node* current = head;
            while (current->next != nullptr) {
                current = current->next;
            }

            // 插入新结点到最后
            current->next = newNode;
        }
    }

    // 显示链表中的元素
    void display() {
        if (head == nullptr) {
            cout << "链表为空" << endl;
        } else {
            Node* current = head;
            while (current != nullptr) {
                cout << current->data << " ";
                current = current->next;
            }
            cout << endl;
        }
    }

    // 合并两个有序链表
    LinkedList merge(LinkedList& listA, LinkedList& listB) {
        LinkedList listC;

        Node* pA = listA.head;
        Node* pB = listB.head;

        // 比较两个链表的结点值,将较小的值插入到新链表中,并移动对应链表的指针
        while (pA != nullptr && pB != nullptr) {
            if (pA->data <= pB->data) {
                listC.insert(pA->data);
                pA = pA->next;
            } else {
                listC.insert(pB->data);
                pB = pB->next;
            }
        }

        // 将剩余的结点插入到新链表中
        while (pA != nullptr) {
            listC.insert(pA->data);
            pA = pA->next;
        }

        while (pB != nullptr) {
            listC.insert(pB->data);
            pB = pB->next;
        }

        return listC;
    }
};

int main() {
    LinkedList listA;
    LinkedList listB;

    // 生成有序链表A
    for (int i = 1; i <= 5; i++) {
        listA.insert(i * 2);
    }

    // 生成有序链表B
    for (int i = 0; i < 5; i++) {
        listB.insert(i * 3 + 1);
    }

    cout << "链表LA:"<<endl;
    listA.display();

    cout << "链表LB:"<<endl;
    listB.display();

    LinkedList listC = listC.merge(listA, listB);
    cout << "链表LC:"<<endl;
    listC.display();

    return 0;
}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值