通过C++编程语言实现“数据结构”课程中的链表

下面是用C++实现单链表的完整代码,包含插入、删除、遍历、反转等核心操作。

#include 
using namespace std;

// 链表节点结构体
struct Node {
    int data;       // 数据域
    Node* next;     // 指针域
    
    Node(int val) : data(val), next(nullptr) {}
};

// 链表类
class LinkedList {
private:
    Node* head;     // 头节点指针

public:
    // 构造函数初始化空链表
    LinkedList() : head(nullptr) {}

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

    // 在链表头部插入新节点
    void insertAtHead(int val) {
        Node* newNode = new Node(val);
        newNode->next = head;
        head = newNode;
    }

    // 在链表尾部插入新节点
    void insertAtTail(int val) {
        Node* newNode = new Node(val);
        
        if (head == nullptr) {
            head = newNode;
            return;
        }

        Node* current = head;
        while (current->next != nullptr) {
            current = current->next;
        }
        current->next = newNode;
    }

    // 删除指定值的节点
    void deleteNode(int val) {
        if (head == nullptr) return;

        // 处理头节点
        if (head->data == val) {
            Node* temp = head;
            head = head->next;
            delete temp;
            return;
        }

        Node* current = head;
        while (current->next != nullptr) {
            if (current->next->data == val) {
                Node* temp = current->next;
                current->next = temp->next;
                delete temp;
                return;
            }
            current = current->next;
        }
    }

    // 查找节点是否存在
    bool search(int val) {
        Node* current = head;
        while (current != nullptr) {
            if (current->data == val) {
                return true;
            }
            current = current->next;
        }
        return false;
    }

    // 反转链表(迭代法)
    void reverse() {
        Node* prev = nullptr;
        Node* current = head;
        Node* next = nullptr;

        while (current != nullptr) {
            next = current->next;  // 保存下一个节点
            current->next = prev;  // 反转指针
            prev = current;         // 移动prev
            current = next;         // 移动current
        }
        head = prev;  // 更新头指针
    }

    // 显示链表内容
    void display() {
        Node* current = head;
        while (current != nullptr) {
            cout << current->data << " -> ";
            current = current->next;
        }
        cout << "NULL" << endl;
    }
};

// 测试主函数
int main() {
    LinkedList list;

    // 插入测试
    list.insertAtTail(1);
    list.insertAtTail(2);
    list.insertAtTail(3);
    list.insertAtHead(0);
    cout << "原始链表: ";
    list.display();  // 输出: 0 -> 1 -> 2 -> 3 -> NULL

    // 删除测试
    list.deleteNode(2);
    cout << "删除2后: ";
    list.display();  // 输出: 0 -> 1 -> 3 -> NULL

    // 查找测试
    cout << "查找3: " << (list.search(3) ? "存在" : "不存在") << endl;  // 存在
    cout << "查找5: " << (list.search(5) ? "存在" : "不存在") << endl;  // 不存在

    // 反转测试
    list.reverse();
    cout << "反转后: ";
    list.display();  // 输出: 3 -> 1 -> 0 -> NULL

    return 0;
}

你可能感兴趣的:(数据结构,c++,链表)