C++ 实现双向链表

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

typedef int DataType;

struct ListNode
{
    ListNode* prev;
    ListNode* next;
    DataType data;
    ListNode(DataType x)
        :data(x)
        ,next(NULL)
        ,prev(NULL)
    {
        ;
    }
};                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        

typedef ListNode Node;

class List
{
public:
    List()
        :_head(NULL)
        , _tail(NULL)
    {}

    void PushBack(const DataType x)
    {
        //1.链表为空
        if (_head == NULL)
        {
            _head = Buynode(x);
            _tail = _head;
        }
        //2.链表不为空
        else
        {
            Node* tmp = _tail;
            _tail->next = Buynode(x);
            _tail = _tail->next;
            _tail->prev = tmp;
        }
    }

    void PushFront(const DataType&x)
    {
        //1.链表为空
        if (_head == NULL)
        {
            _head = Buynode(x);
            _tail = _head;
        }
        //2.链表不为空
        else
        {
            Node* tmp = _head;
            _head->prev = Buynode(x);
            _head = _head->prev;
            _head->next = tmp;
        }
    }

    void PopBack()
    {
        //1.链表为空
        if (_tail == NULL)
        {
            return;
        }
        //2.只有一个节点
        else if (_tail == NULL)
        {
            delete _tail;
            _head = _tail = NULL;
        }
        //3.有多个节点
        else
        {
            Node* tmp = _tail;
            _tail = _tail->prev;
            _tail->next = NULL;
            delete tmp;
        }
    }

    void PopFront()
    {
        //1.链表为空
        if (_head == NULL)
        {
            return;
        }
        //2.只有一个节点
        else if (_head->next == NULL)
        {
            delete _head;
            _head = _tail = NULL;
        }
        //3.有多个节点
        else
        {
            Node* tmp = _head;
            _head = _head->next;
            _head->prev = NULL;
            delete tmp;
        }
    }

    Node* Find(const DataType& x)
    {
        Node* cur = _head;
        while (cur)
        {
            if (cur->data == x)
            {
                return cur;
            }
            cur = cur->next;
        }
        return cur;
    }

    void Insert(Node* pos, const DataType&x)//目标节点前插
    {
        assert(pos);
        //目标节点是头结点直接前插
        if (pos == _head)
        {
            PushFront(x);
        }
        else
        {
            Node*tmp = Buynode(x);
            tmp->prev = pos->prev;
            pos->prev->next = tmp;
            tmp->next = pos;
            pos->prev = tmp;
        }
    }

    void Erase(Node* pos)
    {
        assert(pos);
        //1.要删除节点是头结点直接调用头删
        if (pos == _head)
        {
            PopFront();
        }
        //2.目标节点是尾节点直接调用尾删
        else if (pos == _tail)
        {
            PopBack();
        }
        else
        {
            pos->prev->next = pos->next;
            pos->next->prev = pos->prev;
            delete pos;
            pos = NULL;
        }
    }

    size_t Size()   //求双链表节点个数
    {
        if (_head == NULL)
        {
            return 0;
        }
        size_t count = 0;
        Node* cur = _head;

        while (cur)
        {
            cur = cur->next;
            count++;
        }
        return count;
    }

    bool Empty()const   //判断链表是否为空
    {
        if (_head == _tail == NULL)
        {
            return true;
        }
        return false;
    }

    List(const List& l)     //链表的拷贝构造
        :_head(NULL)
        ,_tail(NULL)
    {
        Node* tmp = l._head;
        while (tmp)
        {   
            PushBack(tmp->data);
            tmp = tmp->next;
        }
    }

    void Swap(List &l)
    {
        swap(_head, l._head);
        swap(_tail, l._tail);
    }

    List&operator = (const List& l) //赋值运算符重载
    {
        if (this != &l)
        {
            List tmp(l);
            Swap(tmp);
        }
        return *this;
    }

    void Display()const
    {
        if (_head == NULL)
        {
            cout << "NULL" << endl;
        }

        Node* cur = _head;
        while (cur)
        {
            cout << cur->data << "->";
            cur = cur->next;
        }   
        cout << endl;

        Node* tmp = _tail;
        while (tmp)
        {
            cout << tmp->data << "->";
            tmp = tmp->prev;
        }
        cout << endl;
    }

private:
    Node* Buynode(const DataType& data)
    {
        return new Node(data);
    }

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

void test1()
{
    List lel;
    lel.PushBack(1);
    lel.PushBack(2);
    lel.PushBack(3);
    lel.PushBack(4);
    lel.Display();
    lel.PushFront(0);
    lel.Display();
    lel.PopFront();
    lel.PopBack();
    lel.Display();
    Node* aim = lel.Find(3);
    lel.Insert(aim, 8);
    lel.Display();
    lel.Erase(aim);
    lel.Display();

    List tet;
    tet = lel;
    tet.Display();

    int i = lel.Empty();
    cout << i << endl;
}

int main()
{
    test1();
    system("pause");
    return 0;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Fly_bit

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值