4.C++实现双向链表

习题4.用C++实现一个双向链表

4.用C++实现一个双向链表

 struct Node
 {
 		int data;
 		Node * pre;
 		Node * next;
 }; 
 逆置一个单链表
 逆置双向链表

 class List
 {
 public:
 		List();
 		~List();
				
        void push_front(int data);//在头部进行插入
               
        void push_back(int data);//在尾部进行插入
                
        void pop_front();//在链表头部进行删除
                  
        void pop_back();//在链表的尾部进行删除
        
        bool find(int data);//在链表中进行查找
        
        void insert(int pos, int data);//在指定位置后面插入pos
        
        void display() const; //打印链表
         		
 		void erase(int data);//删除一个指定的节点			 		
 		
 private:
 		Node * _head;
 		Node * _tail;
 		int    _size;
 };		

解题思路:

提示的内容已经很明细了,注意细节。

#include <iostream>

using std::cout;
using std::endl;

struct Node
{
    Node(int data = 0, Node *pre = nullptr, Node *next = nullptr)
    : _data(data)
    , _pre(pre)
    , _next(next)
    {
        cout << "Node(int = 0, Node * = nullptr, Node * = nullptr)" << endl;
    }

    ~Node()
    {
        cout << "~Node()" << endl;
    }
    int _data;
    Node *_pre;
    Node *_next;
};

class List
{
public:
    List() 
    : _head(new Node())
    , _tail(new Node())
    , _size(0)
    {
        cout << "List()" << endl;
         _head->_next = _tail;
         _tail->_pre = _head;
    }

    void push_front(const int &data)
    {
        Node *newNode = new Node(data);
        newNode->_next = _head->_next;
        newNode->_pre = _head;
        _head->_next->_pre = newNode;
        _head->_next = newNode;
        ++_size;
    }

    void push_back(const int &data)
    {
        Node *newNode = new Node(data);
        newNode->_next = _tail;
        newNode->_pre = _tail->_pre;
        _tail->_pre->_next = newNode;
        _tail->_pre = newNode;
        ++_size;
    }

    void pop_front()
    {
        if(_size>0)
        {
            Node *pNode = _head->_next;
            _head->_next = pNode->_next;
            pNode->_next->_pre = _head;
            --_size;
            delete pNode;
            pNode = nullptr;
        }
        else
        {
            cout << "List is empty, cannot pop_front data !" << endl;
        }
    }
    void pop_back()
    {
        if(_size > 0)
        {
            Node *pNode = _tail->_pre;
            pNode->_pre->_next = _tail;
            _tail->_pre = pNode->_pre;
            --_size;
            delete pNode;
            pNode = nullptr;
        }
        else
        {
            cout << "List is empty, cannot pop_back data !" << endl;
        }
    }
    bool find(int data)
    {
        if(_size > 0)
        {
            Node *pNode = new Node;
            pNode = _head->_next;
            while(pNode && pNode != _tail)
            {
                if(pNode->_data == data)
                {
                    return true;
                }
                pNode = pNode->_next;
            }
            return false;
        }
        else
        {
            cout << "List is empty, cannot find data!" << endl;
            return false;
        }
    }
    void insert(int pos, const int &data)
    {
        if(pos < 0 || pos > _size)
        {
            cout << "插入位置错误!" << endl;
            return;
        }
        Node *pNode = _head->_next;
        while(pos)
        {
            pNode = pNode->_next;
            pos--;
        }
        Node *newNode = new Node(data);
        newNode->_pre = pNode;
        newNode->_next = pNode->_next;
        pNode->_next = newNode;
        pNode->_next = newNode;
        ++_size;
    }
    void display() const
    {
        if(_size > 0)
        {
            Node *pNode = _head->_next;
            while(pNode && pNode != _tail)
            {
                cout << pNode->_data << " ";
                pNode = pNode->_next;
            }
            cout << endl;
        }
        else
        {
            cout << "List is empty, cannot print!!" << endl;
        }
    }
    void erase(const int &data)
    {
        if(_size > 0 )
        {
            Node *pNode = new Node;
            pNode =  _head->_next;

            while(pNode && pNode != _tail)
            {
                if(pNode->_data == data)
                {
                    pNode->_pre->_next = pNode->_next;
                    pNode->_next->_pre = pNode->_pre;
                    --_size;
                    delete pNode;
                    pNode = nullptr;
                    return;
                }
                pNode = pNode->_next;
            }
            cout << "cannot find data!" << endl;
        }
        else
        {
            cout << "List is empty, cant erase!!" << endl;
        }
    }

    ~List()
    {
        cout << "~List()" << endl;
        Node *deleteNode = _head->_next;
        while(deleteNode)
        {
            Node *deleteNextNode = deleteNode->_next;
            delete deleteNode;
            deleteNode = nullptr;
            deleteNode = deleteNextNode;
        }
        delete _head;
        _head = nullptr;
    }
private:
    Node *_head;  //头指针
    Node *_tail;  //尾指针
    int _size;    //链表大小
};

int main()
{
    List lis;
    cout << "在链表的头部插入三个元素1,2,3:" << endl;
    lis.push_front(1);
    lis.push_front(2);
    lis.push_front(3);
    cout << "打印链表" << endl;
    lis.display();
    cout << endl;

    cout << "在链表的尾部插入三个元素6,7,8" << endl;
    lis.push_back(6);
    lis.push_back(7);
    lis.push_back(8);
    cout << "打印链表" << endl;
    lis.display();
    cout << endl;

    cout << "在链表的头部进行一次删除" << endl;
    lis.pop_front();
    cout << "打印链表" << endl;
    lis.display();
    cout << endl;

    cout << "在链表的尾部进行一次删除" << endl;
    lis.pop_back();
    cout << "打印链表" << endl;
    lis.display();
    cout << endl;

    cout << "在链表中查找元素6:" << endl;
    cout << "flag = " << lis.find(6) << endl;
    cout << endl;

    cout << "在链表中的中间进行插入,insert(3,100) : " << endl;
    lis.insert(3, 100);
    cout << "打印链表" << endl;
    lis.display();
    cout << endl;

    cout << "删除链表中元素为6的数据" << endl;
    lis.erase(6);
    cout << "打印链表" << endl;
    lis.display();
 
    return 0;
}
结果:
    
Node(int = 0, Node * = nullptr, Node * = nullptr)
Node(int = 0, Node * = nullptr, Node * = nullptr)
List()
在链表的头部插入三个元素1,2,3:
Node(int = 0, Node * = nullptr, Node * = nullptr)
Node(int = 0, Node * = nullptr, Node * = nullptr)
Node(int = 0, Node * = nullptr, Node * = nullptr)
打印链表
3 2 1 

在链表的尾部插入三个元素6,7,8
Node(int = 0, Node * = nullptr, Node * = nullptr)
Node(int = 0, Node * = nullptr, Node * = nullptr)
Node(int = 0, Node * = nullptr, Node * = nullptr)
打印链表
3 2 1 6 7 8 

在链表的头部进行一次删除
~Node()
打印链表
2 1 6 7 8 

在链表的尾部进行一次删除
~Node()
打印链表
2 1 6 7 

在链表中查找元素6:
flag = Node(int = 0, Node * = nullptr, Node * = nullptr)
1

在链表中的中间进行插入,insert(3,100) : 
Node(int = 0, Node * = nullptr, Node * = nullptr)
打印链表
2 1 6 7 100 

删除链表中元素为6的数据
Node(int = 0, Node * = nullptr, Node * = nullptr)
~Node()
打印链表
2 1 7 100 
~List()
~Node()
~Node()
~Node()
~Node()
~Node()
~Node()
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值