链表,未返回上一个

Linked List, not getting previous returned

本文关键字:上一个 返回 链表      更新时间:2023-10-16

我创建了一个链表,当我添加到列表的前面或后面时,它就可以工作了。我把它们打印出来,一切看起来都很好。然后,我从列表的前面删除了(pop_front),由于某种原因,行temp = head->getPrevious();(list.cpp)返回值0x000000,所以很明显,它实际上并没有得到指向前一个节点的指针。我穿过它,什么也看不见。任何帮助都会很棒!

主要.cpp

#include "stdafx.h"
#include "List.h"

int main()
{
    List partsList;
    partsList.push_front(22);
    partsList.push_front(25);
    partsList.push_front(32);
    partsList.push_back(100);
    partsList.display();
    cout << "now we are going to remove the first item in the list" << endl;
    system("PAUSE");
    partsList.pop_front();
    partsList.display();
    system("PAUSE");
    cout << "now we are going to remove the LAST item from the list" << endl;
    partsList.pop_back();
    partsList.display();
    system("PAUSE");
    return 0;
}

List.h

#pragma once
#include "node.h"
#include <iostream>
using namespace std;
class List
{
private:
    int listSize;
    Node* n;
    Node* temp;
    Node* head;
    Node* tail;
public:
    List();
    void push_front(int);
    void push_back(int);
    void pop_front();
    void pop_back();
    void display();
    ~List();
};

List.cpp

#include "stdafx.h"
#include "List.h"

List::List()
{
}
void List::push_front(int dat)
{
    if (listSize == 0) {
        n = new Node;
        n->setData(dat);
        listSize++;
        temp = n;
        head = n;
        tail = n;
    }
    else {
        n = new Node;
        n->setData(dat);
        listSize++;
        temp = head;
        head = n;
        n->setNext(temp);
        n->setPrevious(nullptr);
        temp->setPrevious(n);
        temp = n;
    }
}
void List::push_back(int dat)
{
    if (listSize == 0) {
        n = new Node;
        n->setData(dat);
        listSize++;
        temp = n;
        head = n;
        tail = n;
    }
    else {
        n = new Node;
        n->setData(dat);
        listSize++;
        temp = tail;
        temp->setNext(n);
        n->setPrevious(temp);
// SET NEXT TO NULL
        temp = n;
        tail = temp;
    }
}
void List::pop_front()
{
    temp = head->getPrevious();
    delete head;
    head = temp;
    listSize--;
}
void List::pop_back()
{
    temp = tail->getPrevious();
    delete tail;
    tail = temp;
    tail->setNext(nullptr);
    listSize--;
}
void List::display()
{
    Node* test = head;
    for (int i = 0; i < listSize; i++) {
        cout << test->getData() << endl;
        test = test->getNext();
    }
}

List::~List()
{
}

节点.h

#pragma once
class Node
{
private:
    int data;
    Node* next;
    Node* previous;
public:
    Node();
    int getData();
    void setData(int);
    void setNext(Node*);
    void setPrevious(Node*);
    Node* getPrevious();
    Node* getNext();
    ~Node();
};

Node.cpp

#include "stdafx.h"
#include "Node.h"

Node::Node()
{
}
int Node::getData()
{
    return data;
}
void Node::setData(int dat)
{
    data = dat;
}
void Node::setNext(Node* nextNode)
{
    next = nextNode;
}
void Node::setPrevious(Node* prev)
{
    previous = prev;
}
Node * Node::getPrevious()
{
    return previous;
}
Node * Node::getNext()
{
    return next;
}


Node::~Node()
{
}

List::pop_front()中,您正在调用head->getPrevious();,它显然正在返回nullptr,并将其设置为列表的新头。它应该是getNext()

顺便说一句:不应该在List对象中存储像n和temp这样的临时变量。在操作方法之外不需要它们,它们只会使对象膨胀。