【C++】 双向链表.cpp

双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。


一般我们都构造双向循环链表,因为双向链表解决了单向链表的不足和问题,而单链表因为出现的问题多,所以在面试中经常会考到单链表的问题。


在这里,我用  .cpp对双向链表的基本操作进行实现。


具体代码如下:

  List.h文件:

#pragma once
 
typedef int DataType;
 
struct ListNode  //全部用作公有的就定义为struct,用作私有就定义class
{                 //当然用class,写在public中也可以
    DataType _data;
    ListNode* _next; //前驱指针
    ListNode* _prev; //后继指针
 
    ListNode(DataType x) //构造函数
        :_data(x)
        ,_next(NULL)
        ,_prev(NULL)
    {}
 
};
 
class List
{
public:
    List()
        :_head(NULL)
        ,_tail(NULL)
    {}
 
    ~List()
    {
        Clear();
    }
 
public:
    void PushBack(DataType x)
    {
        if(_head == NULL)
        {
            _head = _tail = new ListNode(x);//调用构造函数
        }
        else
        {
            ListNode* tmp = new ListNode(x);
            _tail->_next = tmp;
            tmp->_prev = _tail;
 
            _tail = tmp;
 
        }
    }
 
    void PopBack()
    {
        //没有节点
        //一个节点
        //一个以上节点
        if(_head == NULL)
        {
            return;
        }
        else if(_head == _tail)
        {
            delete _head;
            _head = _tail = NULL;
        }
        else
        {
            ListNode* cur = _tail->_prev;
            delete _tail;
            _tail = cur;
            cur->_next = NULL;
        }
    }
 
    void PushFront(DataType x)
    {
        if(_head == NULL)
        {
            _head = _tail = new ListNode(x);
        }
        else
        {
            ListNode* tmp = new ListNode(x);
            tmp->_next = _head;
            _head = tmp;
        }
    }
 
    void PopFront()
    {
        if(_head == NULL)//空
        {
            return;
        }
        else if(_head == _tail)//一个节点
        {
            delete _head;
            _head = _tail = NULL;
        }
        else        //一个以上节点
        {
            ListNode* del = _head;
            _head = _head->_next;
            _head->_prev = NULL;
            delete del;
        }
    }
 
    void Insert(ListNode* pos,DataType x)
    {
        assert(pos);
 
        if(pos == _tail)
        {
            PushBack(x);
        }
        else
        {
            /*
            ListNode* tmp = new ListNode(x);
            tmp->_next = pos->_next;
            pos->_next->_prev = tmp;  //使用这种两个指针的方式虽然可以达到目的,但是容易出现问题                                      
            pos->_next = tmp;         //首先得考虑pos是否为空,其次若pos->_next为空,再想找到它的prev就会出错
            tmp->_prev = pos;
            */
 
            ListNode* tmp = new ListNode(x);//这样定义两个指针保存前后的节点,就不容易出错
            ListNode* next = pos->_next;
            tmp->_next = next;
            next->_prev = tmp;
            pos->_next = tmp;
            tmp->_prev = pos;
 
        }
    }
 
    ListNode* Find(DataType x)
    {
        if(_head == NULL)
        {
            return NULL;//找不到返回空
        }
        else
        {
            ListNode* cur = _head;
            while(cur)
            {
                if(cur->_data == x)
                {
                    return cur;
                }
                cur = cur->_next;
            }
        }
 
    }
 
    void Erase(ListNode* pos)
    {
        assert(_head);
        assert(pos);
 
        if(pos == _head)//删除头节点
        {
            PopFront();

            /*
            ListNode* del = _head;
            _head = _head->_next;
            _head->_prev = NULL;
            delete del;
            */
        }
        else if(pos == _tail)//删除尾节点
        {
            PopBack();
        }
        else // 删除非头尾节点
        {
            /*
            ListNode* cur = pos->_prev;
            cur->_next = pos->_next;
            pos->_next->_prev = cur;
            delete pos;
            */
            
            ListNode* prev = pos->_prev;
            ListNode* next = pos->_next;
            prev->_next = next;
            next->_prev = prev;
            delete pos;
 
 
        }
    }
 
    void PrintList()//在此用不上前驱节点prev
    {
        ListNode* cur = _head;
        while(cur)
        {
            cout<<cur->_data<<"->";
            cur = cur->_next;
        }
        cout<<"NULL"<<endl;
    }
 
    void Clear()
    {
        ListNode* cur = _head;
        while(cur)
        {
            ListNode* del = cur;
            cur = cur->_next;
            delete del;
        }
    }
     
    //翻转双向链表
    void reverse()
    {
        /*  ①.交换值
        ListNode* begin = _head;
        ListNode* end = _tail;
 
        while(begin != end && begin->_prev != end)//当两个指针相遇或者已经偏离停止
        {
            swap(begin->_data , end->_data);
            begin = begin->_next;
            end = end ->_prev;
        }
        */
 
        //②.交换指针
        ListNode* cur = _head;
         
        while(cur)
        {
            swap(cur->_prev , cur->_next);//把每个节点的前后指针交换
            cur = cur->_prev;
        }
        swap(_head,_tail);
    }
 
private:
    ListNode* _head;
    ListNode* _tail;
 
};
 
void Test()
{
    List l;
    l.PushBack(1);
    l.PushBack(2);
    l.PushBack(3);
    l.PushBack(4);
    l.PushBack(5);
    l.PrintList();
 
    l.PopBack();
    l.PrintList();
     
    l.PushFront(0);
    l.PrintList();
 
    l.PopFront();
    l.PrintList();
 
    l.Insert(l.Find(1),0);
    l.PrintList();
 
    l.Erase(l.Find(1));
    l.PrintList();
 
    l.reverse();
    l.PrintList();
}

List.cpp 文件:(测试)

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

#include "DoubleSList.h"  //双向链表

int main()
{
    Test();
    return 0;
}

我们发现,当遇到问题时不能直接上手去写,为什么?因为我们写的是代码,而不是bug,正确的程序是需要严谨的逻辑为前提,所以在写代码之前需要谨慎的思考各种临界的情况和异常,这是非常之重要的!

本文出自 “Vs吕小布” 博客,谢绝转载!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
数据结构课程设计中,C++双向链表是一个常见的数据结构之一。它是一种线性数据结构,由多个节点组成,每个节点包含两个指针,一个指向前一个节点,一个指向后一个节点。双向链表相比于单向链表,可以实现双向遍历。 在C++中,可以通过定义一个双向链表类来实现双向链表的功能。以下是一个简单的C++双向链表的实现示例: ```cpp #include <iostream> // 双向链表节点定义 class Node { public: int data; Node* prev; Node* next; }; // 双向链表类定义 class DoublyLinkedList { private: Node* head; // 头节点指针 public: DoublyLinkedList() { head = nullptr; } // 在链表头部插入节点 void insertAtHead(int value) { Node* newNode = new Node(); newNode->data = value; newNode->prev = nullptr; newNode->next = head; if (head != nullptr) { head->prev = newNode; } head = newNode; } // 在链表尾部插入节点 void insertAtTail(int value) { Node* newNode = new Node(); newNode->data = value; newNode->next = nullptr; if (head == nullptr) { newNode->prev = nullptr; head = newNode; return; } Node* temp = head; while (temp->next != nullptr) { temp = temp->next; } temp->next = newNode; newNode->prev = temp; } // 打印链表元素 void printList() { Node* temp = head; while (temp != nullptr) { std::cout << temp->data << " "; temp = temp->next; } std::cout << std::endl; } }; int main() { DoublyLinkedList dll; dll.insertAtHead(3); dll.insertAtHead(2); dll.insertAtHead(1); dll.printList(); // 输出:1 2 3 dll.insertAtTail(4); dll.insertAtTail(5); dll.printList(); // 输出:1 2 3 4 5 return 0; } ``` 以上是一个简单的C++双向链表的实现示例。你可以通过调用`insertAtHead`和`insertAtTail`方法来插入节点,通过调用`printList`方法来打印链表元素。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值