C++实现双链表

C++简单实现顺序表和单链表
双链表

#include<iostream>
#include<assert.h>
using namespace std;

typedef int DataType;

struct ListNode
{
    ListNode* _next;
    ListNode* _prev;
    DataType _data;

    ListNode(DataType x)
        :_next(NULL)
        , _prev(NULL)
        , _data(x)
    {}
};

class List
{
    typedef ListNode Node;
public:
    List()
        :_head(NULL)
        , _tail(NULL)
    {}
    List(const List& l)
        :_head(NULL)
        , _tail(NULL)
    {
        Copy(l);
    }
    List& operator=(const List& l)
    {
        Destory();
        Copy(l);
        return *this;
    }
    ~List()
    {
        Destory();
    }
    void Copy(const List& l)
    {
        Node* cur = l._head;
        while (cur)
        {
            PushBack(cur->_data);
            cur = cur->_next;
        }
    }
    void Destory()
    {
        if (_head)
        {
            Node* cur = _head;
            while (_head)
            {
                cur = _head;
                _head = _head->_next;
                delete cur;
            }
            _head = _tail = NULL;
        }
    }
    void PushBack(DataType x)
    {
        if (_tail == NULL)
        {
            _head = _tail = new Node(x);
        }
        else
        {
            Node* tmp = new Node(x);
            _tail->_next = tmp;
            tmp->_prev = _tail;
            _tail = tmp;
        }
    }
    void PopBack()
    {
        Erase(_tail);
    }
    void PushFront(DataType x)
    {
        if (_head == NULL)
        {
            _head = _tail = new Node(x);
        }
        else
        {
            Insert(_head, x);
        }
    }
    void PopFront()
    {
        Erase(_head);
    }
    // 在pos的前面插入一个 
    void Insert(Node* pos, DataType x)
    {
        assert(pos);
        if (pos == _head)
        {
            Node* tmp = new Node(x);
            tmp->_next = _head;
            _head->_prev = tmp;
            _head = tmp;
        }
        else
        {
            Node* cur = pos;
            Node* prev = cur->_prev;
            Node* tmp = new Node(x);
            prev->_next = tmp;
            tmp->_next = cur;

            cur->_prev = tmp;
            tmp->_prev = prev;
        }
    }
    void Erase(Node* pos)
    {
        assert(pos);
        Node* prev = pos->_prev;
        Node* next = pos->_next;

        if (prev == NULL && next == NULL)
        {
            assert(pos == _head);
            _head = _tail = NULL;
        }
        else if (prev == NULL)
        {
            _head = next;
            _head->_prev = NULL;
        }
        else if (next == NULL)
        {
            _tail = prev;
            _tail->_next = NULL;
        }
        else
        {
            prev->_next = next;
            next->_prev = prev;
        }

        delete pos;
    }

    Node* Find(DataType x)
    {
        Node* cur = _head;
        while (cur)
        {
            if (cur->_data == x)
                return cur;
            cur = cur->_next;
        }
        return NULL;
    }
    void Reverse()
    {
        Node* cur = _head;
        while (cur)
        {
            swap(cur->_prev, cur->_next);
            cur = cur->_prev;
        }

        swap(_head, _tail);
    }
    void Print()
    {
        Node* cur = _head;
        while (cur)
        {
            cout << cur->_data << " ";
            cur = cur->_next;
        }
        cout << endl;
    }

private:
    Node* _head;
    Node* _tail;
};

你可能感兴趣的:(CPP)