> For the complete documentation index, see [llms.txt](https://xd96dx.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xd96dx.gitbook.io/docs/c-c++/stl-shi-xian/rong-qi-containers/list-shuang-xiang-lian-biao.md).

# List(双向链表)

```cpp
#include <iostream>
#include <stdexcept>

namespace XD
{
    template <typename T>
    class List {
    private:
        struct Node {
            T data;
            Node* prev;
            Node* next;

            Node(const T& value) : data(value), prev(nullptr), next(nullptr) {}
        };

        Node* head;  // 指向链表头部
        Node* tail;  // 指向链表尾部
        size_t size; // 链表的大小

    public:
        List() : head(nullptr), tail(nullptr), size(0) {}

        ~List() {
            clear();
        }

        // 添加到链表尾部
        void push_back(const T& value) {
            Node* newNode = new Node(value);
            if (tail) {
                tail->next = newNode;
                newNode->prev = tail;
                tail = newNode;
            }
            else {
                head = tail = newNode;
            }
            ++size;
        }

        // 添加到链表头部
        void push_front(const T& value) {
            Node* newNode = new Node(value);
            if (head) {
                head->prev = newNode;
                newNode->next = head;
                head = newNode;
            }
            else {
                head = tail = newNode;
            }
            ++size;
        }

        // 删除链表尾部元素
        void pop_back() {
            if (!tail) throw std::out_of_range("List is empty");
            Node* temp = tail;
            tail = tail->prev;
            if (tail) {
                tail->next = nullptr;
            }
            else {
                head = nullptr;
            }
            delete temp;
            --size;
        }

        // 删除链表头部元素
        void pop_front() {
            if (!head) throw std::out_of_range("List is empty");
            Node* temp = head;
            head = head->next;
            if (head) {
                head->prev = nullptr;
            }
            else {
                tail = nullptr;
            }
            delete temp;
            --size;
        }

        // 反转
        void reverse() {
            if (!head || !head->next) {
                // 空链表直接返回
                return;
            }

            Node* current = head;
            Node* temp = nullptr;

            while (current) {
                temp = current->prev;
                current->prev = current->next;
                current->next = temp;
                current = current->prev;
            }

            temp = head;
            head = tail;
            tail = temp;
        }

        // 清空链表
        void clear() {
            while (head) {
                pop_front();
            }
        }

        // 获取链表大小
        size_t get_size() const {
            return size;
        }

        // 检查链表是否为空
        bool empty() const {
            return size == 0;
        }

        // 打印链表内容
        void print_() const {
            Node* current = head;
            while (current) {
                std::cout << current->data << " ";
                current = current->next;
            }
            std::cout << std::endl;
        }
    };
}

```

主要写了链表算法相关的, 构造的部分和注意的点, 跟vector差不多
