将项目添加到双向链表的背面时遇到问题

Trouble Adding an item to the back of a doubly linked list

本文关键字:背面 遇到 问题 双向链表 项目 添加      更新时间:2023-10-16

我是双向链表的新手。 我正在处理几段代码:一个函数将项目添加到前面,另一个函数将项目添加到后面,以及分别从前到后和从后到前输出链表的正向和反向显示方法。
我在输出中遇到了一个错误,我试图理解。 似乎我的addFront和显示功能正在工作,但我的addBack可能是我的错误所在。 我在这篇文章的底部发布了我的输出是什么以及它显示的内容。这是我到目前为止的代码。

#include<iostream>
using namespace std;
class doubleLinkedList
{
private:
    class node
    {
    public:
        int data;
        node* next;
        node* prev;
        node(int x)
        {
            data = x;
            next = NULL;
            prev = NULL;
        }
    };
public:
    node* head;
    node* tail;
    int count;

    doubleLinkedList();        //default constructor
    ~doubleLinkedList();      //destructor
    void displayForward();    //display items from front to back
    void displayBackward();   //display items from back to front
    void addFront(int);      //add item to front of linked list
    void addBack(int);       //add item to back of linked list
    int removeFront();       //remove item from front of linked list
    int removeBack();        //remove item from back of linked list
};
//constructor
doubleLinkedList::doubleLinkedList(){
    head = tail = NULL;
    count = 0;
}

//destructor
doubleLinkedList::~doubleLinkedList(){
    node* current = head;
    while(current != NULL)
    {
        node* previous = current;
        current = current->next;
        delete previous;
    }
    head = tail = NULL;
    count = 0;
}

//display items in linked list from front to back
void doubleLinkedList::displayForward(){
    node* pCurrent = head;
    while (pCurrent != NULL)
    {
        cout << pCurrent->data << " ";
        pCurrent = pCurrent->next;
    }
    cout << count;
}

//display items in linked list from back to front
void doubleLinkedList::displayBackward(){
    node* pCurrent = tail;
    while (pCurrent != NULL)
    {
        cout <<pCurrent->data << " ";
        pCurrent = pCurrent->prev;
    }
    cout << count;
}

//add item to front of linked list
void doubleLinkedList::addFront(int x){
    node* n = new node(x);
    n->next = head;
    n->prev = NULL;
    if (head != NULL)
        head->prev = n;
        head = n;
        count++;
    if (tail == NULL)
        tail = n;
}
void doubleLinkedList::addBack(int x){
    node* n = new node(x);
    n->next = NULL;
    n->prev = tail;
    tail = n;
    count++;
}
//

Code:////////////////////

int main()
{
    doubleLinkedList list;
    list.addBack(40);
    list.addBack(50);
    list.addBack(60);
    list.addFront(30);
    list.addFront(20);
    list.addBack(70);
    list.addBack(80);
    list.addFront(10);
    list.displayForward();  //10 20 30 8   (the 8 value is the count/size i'm keeping track of)
        cout << endl;
    list.displayBackward(); //80 70 60 50 40 8
        cout << endl;
        system("pause");
        return 0;
        }

我的输出应显示 10 20 30 40 50 60 70 80和 80 70 60 50 40 30 20 10
而是我的 displayForward 显示我添加到前面的项目,我的 displayBackward 显示我添加到后面的项目。

你忘了将旧尾巴设置为指向新尾巴的下一个指针。因此,在浏览列表时,最后一个节点的下一个节点仍将指向 NULL。

此外,当添加到背面时,您

没有检查头部是否为空,因此在添加到背面时,您只更新了尾部,结果最终有两个单独的列表。

所以

你在"end"中添加了 40、50、60,所以尾部指针被设置并相应地更新,但直到你向列表中添加 30 个,继续在前面添加元素,指针相应地更新,但结果头和尾实际上并没有连接。

void doubleLinkedList::addBack(int x){       
    node* n = new node(x);
    if (head == nullptr)
        head = n; //this was your main problem!
    if (tail != nullptr)
        tail->next = n;
    n->next = nullptr;
    n->prev = tail;
    tail = n;
    count++;
}

由于您似乎正在编写C++代码,因此我建议您也习惯使用 nullptr 而不是 NULL。

http://www.codeproject.com/Articles/570638/Ten-Cplusplus11-Features-Every-Cplusplus-Developer