麻烦将项目添加到双向链接列表的后面

时间:2014-02-28 04:38:55

标签: c++ insert linked-list double tail

我是双向链接列表的新手。我正在处理几段代码:一个函数将项添加到前面,另一个函数将项添加到后面,以及正向和反向显示方法,从前到后和后面输出链接列表,分别
我在输出中遇到了一个错误,我试图理解。看来我的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++;
}

////////////////////我的测试代码:///////////////////

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显示我添加到Front的项目,而我的displayBackward显示我添加到后面的项目。

1 个答案:

答案 0 :(得分:1)

您忘记将旧尾巴设置为指向新尾巴的下一个指针。因此,当遍历列表时,倒数第二个节点仍将指向NULL。

此外,当添加到后面时,你没有检查head是否为null,所以当添加到后面时,你只更新了尾端,最后得到了两个单独的列表。

所以你在“结束”中添加了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

相关问题