链表,未返回上一个
Linked List, not getting previous returned
我创建了一个链表,当我添加到列表的前面或后面时,它就可以工作了。我把它们打印出来,一切看起来都很好。然后,我从列表的前面删除了(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这样的临时变量。在操作方法之外不需要它们,它们只会使对象膨胀。
相关文章:
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- C ++ usleep 在 Linux 上立即返回
- C++,class,一个返回对象的函数
- C++ 这里有一个返回 (24) 的布尔返回类型函数
- 我有一个返回字符串向量的函数.它需要两个字符串,并且返回一个字符串中缺少的字符串
- 两个相同的重载运算符[]一个返回引用
- 给定一个整数 N>0,区间 [0, 2^N) 中有多少个整数正好有 N-1 个设置位?编写一个返回正确答案的简短函数
- 类模板上一个特定函数的未解析外部符号
- 错误:"int pow(double,int)"与上一个声明int pov(double a,int
- 如何查看提升集中<cpp_int>的下一个和上一个元素
- 我想知道我将如何实现 + 运算符重载.我已经从我上一个问题中计算出 += 运算符重载
- 如何删除上一个节点?
- 插入指针数组会删除上一个结果
- boost::asio::async_read 在命名管道上不断返回 EOF
- 只有在上一个线程返回后才启动一个新的"wxThread"
- mbtowc在osx上总是返回一个字节
- 我是否应该将最后一个“返回”语句封装为“else { return .. }',如果它在逻辑上是可选的
- 我希望计时器只在上一个调用返回或完成执行时调用代码
- 链表,未返回上一个
- 成员函数返回变量的上一个值