编辑代码

#include <iostream>
using namespace std;

// 链表节点结构
struct Node {
    int data;        // 数据字段
    Node* next;     // 指向下一个节点的指针
    Node(int value) : data(value), next(nullptr) {} // 构造函数
};

class LinkedListSeq {
private:
    Node* head;      // 链表的头指针
    int length;      // 当前元素个数

public:
    LinkedListSeq() : head(nullptr), length(0) {} // 构造函数

    // 插入元素
    void insert(int index, int value) {
        if (index < 0 || index > length) {
            cout << "Index out of bounds." << endl;
            return; // 检查越界
        }

        Node* newNode = new Node(value); // 创建新节点
        if (index == 0) { // 插入到头部
            newNode->next = head;
            head = newNode;
        } else {
            Node* current = head;
            for (int i = 0; i < index - 1; ++i) { // 找到插入位置
                current = current->next;
            }
            newNode->next = current->next; // 新节点指向当前节点的下一个节点
            current->next = newNode; // 当前节点指向新节点
        }
        length++;
    }

    // 删除元素
    void remove(int index) {
        if (index < 0 || index >= length) {
            cout << "Index out of bounds." << endl;
            return; // 检查越界
        }

        Node* toDelete;

        if (index == 0) {
            toDelete = head; // 删除头节点
            head = head->next; // 头指针指向下一个节点
        } else {
            Node* current = head;
            for (int i = 0; i < index - 1; ++i) { // 找到要删除节点的前一个节点
                current = current->next;
            }
            toDelete = current->next; // 要删除的节点
            current->next = toDelete->next; // 跳过要删除的节点
        }
        delete toDelete; // 释放内存
        length--;
    }

    // 修改元素
    void update(int index, int value) {
        if (index < 0 || index >= length) {
            cout << "Index out of bounds." << endl;
            return; // 检查越界
        }

        Node* current = head;
        for (int i = 0; i < index; ++i) { // 找到要修改的节点
            current = current->next;
        }
        current->data = value; // 修改数据
    }

    // 查找元素
    int search(int value) {
        Node* current = head;
        for (int i = 0; i < length; ++i) {
            if (current->data == value) {
                return i; // 返回索引
            }
            current = current->next; // 移动到下一个节点
        }
        return -1; // 未找到
    }

    // 打印顺序表
    void print() {
        Node* current = head;
        while (current) {
            cout << current->data << " ";
            current = current->next;
        }
        cout << endl;
    }

    ~LinkedListSeq() { // 析构函数,释放内存
        Node* current = head;
        while (current) {
            Node* nextNode = current->next;
            delete current;
            current = nextNode;
        }
    }
};

int main() {
    LinkedListSeq seqList;

    // 插入元素
    seqList.insert(0, 10); // [10]
    seqList.insert(1, 20); // [10, 20]
    seqList.insert(1, 15); // [10, 15, 20]
    seqList.print(); // 输出: 10 15 20 

    // 修改元素
    seqList.update(1, 25); // [10, 25, 20]
    seqList.print(); // 输出: 10 25 20 

    // 查找元素
    int index = seqList.search(20); // 查找20的索引
    cout << "Value 20 is at index: " << index << endl; // 输出: Value 20 is at index: 2

    // 删除元素
    seqList.remove(1); // [10, 20]
    seqList.print(); // 输出: 10 20 

    return 0;
}