C++中的链表操作

在C++中,链表是一种常见的数据结构,它由一系列节点组成,每个节点包含数据部分和指向下一个节点的指针。C++标准库(STL)中提供了std::liststd::forward_list两种链表实现,分别对应双向链表和单向链表。此外,也可以通过手动实现链表来加深对链表的理解。

1. C++标准库中的链表

(1)std::list(双向链表)

std::list是C++标准库中的双向链表实现,每个节点包含指向前一个节点和后一个节点的指针。它支持高效的插入和删除操作,但随机访问效率较低。

  • 基本操作

    #include <iostream>
    #include <list>
    
    int main() {
        std::list<int> myList;
    
        // 插入元素
        myList.push_back(10); // 在链表尾部插入
        myList.push_front(20); // 在链表头部插入
        myList.insert(myList.begin() + 1, 30); // 在指定位置插入
    
        // 遍历链表
        for (int value : myList) {
            std::cout << value << " ";
        }
        std::cout << std::endl;
    
        // 删除元素
        myList.pop_back(); // 删除尾部元素
        myList.pop_front(); // 删除头部元素
        myList.erase(myList.begin() + 1); // 删除指定位置的元素
    
        // 遍历链表
        for (int value : myList) {
            std::cout << value << " ";
        }
        std::cout << std::endl;
    
        return 0;
    }
(2)std::forward_list(单向链表)

std::forward_list是C++11引入的单向链表实现,每个节点只包含指向下一个节点的指针。它比std::list更轻量,但只能单向遍历。

  • 基本操作

    #include <iostream>
    #include <forward_list>
    
    int main() {
        std::forward_list<int> myList;
    
        // 插入元素
        myList.push_front(10); // 在链表头部插入
        myList.push_front(20);
        myList.insert_after(myList.begin(), 30); // 在指定位置插入
    
        // 遍历链表
        for (int value : myList) {
            std::cout << value << " ";
        }
        std::cout << std::endl;
    
        // 删除元素
        myList.pop_front(); // 删除头部元素
        myList.erase_after(myList.begin()); // 删除指定位置的元素
    
        // 遍历链表
        for (int value : myList) {
            std::cout << value << " ";
        }
        std::cout << std::endl;
    
        return 0;
    }

2. 手动实现链表

手动实现链表可以帮助更好地理解链表的内部机制。以下是单向链表和双向链表的基本实现。

(1)单向链表
#include <iostream>

// 定义链表节点
struct Node {
    int data;
    Node* next;

    Node(int value) : data(value), next(nullptr) {}
};

// 定义链表类
class LinkedList {
private:
    Node* head;

public:
    LinkedList() : head(nullptr) {}

    // 插入元素到链表尾部
    void append(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 prepend(int value) {
        Node* newNode = new Node(value);
        newNode->next = head;
        head = newNode;
    }

    // 删除元素
    void remove(int value) {
        if (head == nullptr) return;

        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 = temp->next;
            delete temp;
        }
    }

    // 遍历链表
    void print() const {
        Node* current = head;
        while (current != nullptr) {
            std::cout << current->data << " ";
            current = current->next;
        }
        std::cout << std::endl;
    }

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

int main() {
    LinkedList list;

    list.append(10);
    list.append(20);
    list.prepend(5);

    list.print(); // 输出:5 10 20

    list.remove(10);
    list.print(); // 输出:5 20

    return 0;
}
(2)双向链表
#include <iostream>

// 定义链表节点
struct Node {
    int data;
    Node* next;
    Node* prev;

    Node(int value) : data(value), next(nullptr), prev(nullptr) {}
};

// 定义链表类
class DoublyLinkedList {
private:
    Node* head;
    Node* tail;

public:
    DoublyLinkedList() : head(nullptr), tail(nullptr) {}

    // 插入元素到链表尾部
    void append(int value) {
        Node* newNode = new Node(value);
        if (head == nullptr) {
            head = newNode;
            tail = newNode;
        } else {
            tail->next = newNode;
            newNode->prev = tail;
            tail = newNode;
        }
    }

    // 插入元素到链表头部
    void prepend(int value) {
        Node* newNode = new Node(value);
        if (head == nullptr) {
            head = newNode;
            tail = newNode;
        } else {
            newNode->next = head;
            head->prev = newNode;
            head = newNode;
        }
    }

    // 删除元素
    void remove(int value) {
        Node* current = head;
        while (current != nullptr && current->data != value) {
            current = current->next;
        }

        if (current == nullptr) return;

        if (current->prev != nullptr) {
            current->prev->next = current->next;
        } else {
            head = current->next;
        }

        if (current->next != nullptr) {
            current->next->prev = current->prev;
        } else {
            tail = current->prev;
        }

        delete current;
    }

    // 遍历链表
    void print() const {
        Node* current = head;
        while (current != nullptr) {
            std::cout << current->data << " ";
            current = current->next;
        }
        std::cout << std::endl;
    }

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

int main() {
    DoublyLinkedList list;

    list.append(10);
    list.append(20);
    list.prepend(5);

    list.print(); // 输出:5 10 20

    list.remove(10);
    list.print(); // 输出:5 20

    return 0;
}

总结

  • 标准库链表std::liststd::forward_list提供了丰富的功能和高效的插入删除操作,适合大多数应用场景。

  • 手动实现链表:通过手动实现链表,可以加深对链表内部机制的理解,例如节点的创建、插入、删除和内存管理等。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这个懒人

感谢打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值