c++ - 为什么我的模板链接列表不起作用?
问题描述
我无法指定问题所在,也没有收到任何错误。这是我的代码:
节点.h:
#ifndef NODE_H
#define NODE_H
#include <iostream>
using namespace std;
template <typename T>
class LinkedList;
template <typename T>
class Node {
public:
Node(T data) : data(data),previousNode(nullptr),nextNode(nullptr) {}
Node<T>* GetNextNode() const {
return nextNode;
}
void SetNextNode(Node<T>* nextNode) {
this->nextNode = nextNode;
}
Node<T>* GetPreviousNode() const {
return previousNode;
}
void SetPreviousNode(Node<T>* previousNode) {
this->previousNode = previousNode;
}
T GetData() const {
return data;
}
void SetData(T data) {
this->data = data;
}
private:
T data;
Node<T>* previousNode;
Node<T>* nextNode;
};
#endif /* NODE_H */
链接列表.h:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "node.h"
#include <stdexcept>
#include <iostream>
#include <booking.h>
using namespace std;
template<typename T>
class LinkedList {
public:
LinkedList() {
root = nullptr;
end = nullptr;
cursor = end;
}
void insertNode(T data) {
Node<T>* node = new Node<T>(data);
cerr << "entered insert node" << endl;
if (root == nullptr) {
root = node;
node->SetNextNode(end);
end->SetPreviousNode(node);
}else{
node->SetNextNode(cursor);
node->SetPreviousNode(cursor->GetPreviousNode());
if (cursor->GetPreviousNode())
cursor->GetPreviousNode()->SetNextNode(node);
cursor->SetPreviousNode(node);
}
cursor = node;
if (!cursor->GetPreviousNode())
root = cursor;
}
bool isAtEnd() {
return (cursor == end);
};
void step_back() {
if (cursor && cursor->GetPreviousNode()) {
cursor = cursor->GetPreviousNode();
}
}
void advance() {
if (cursor && cursor->GetNextNode()) {
cursor = cursor->GetNextNode(); }
};
T getNode() {
return cursor->GetData();
};
void reset() {
cursor = root;
};
void deleteNode() {
Node<T>* tmpPrevious;
Node<T>* tmpNext;
if (root == nullptr) {
throw underflow_error("empty list...");
} else {
if (cursor->GetPreviousNode() == nullptr) {
if (cursor->GetNextNode() == end) {
delete cursor;
root = nullptr;
end->SetPreviousNode(nullptr);
cursor = end;
} else {
cursor = cursor->GetNextNode();
delete (root);
cursor->SetPreviousNode(nullptr);
root = cursor;
}
} else {
if (cursor->GetNextNode() == end) {
tmpPrevious = cursor->GetPreviousNode();
delete cursor;
cursor = end;
end->SetPreviousNode(tmpPrevious);
tmpPrevious->SetNextNode(end);
} else {
tmpPrevious = cursor->GetPreviousNode();
tmpNext = cursor->GetNextNode();
delete cursor;
tmpPrevious->SetNextNode(tmpNext);
tmpNext->SetPreviousNode(tmpPrevious);
cursor = tmpNext;
}
}
}
return;
};
protected:
Node<T>* root;
Node<T>* cursor;
Node<T>* end;
};
#endif /* LINKEDLIST_H */
预订.h
#ifndef BOOKING_H
#define BOOKING_H
class Booking{
private:
long bookingID;
public:
Booking(long bookingID);
long getBookingID();
};
#endif // BOOKING_H
预订.ccp:
#include "booking.h"
Booking::Booking(long bookingID):bookingID(bookingID){}
long Booking::getBookingID(){
return bookingID;
}
主.cpp:
#include <QCoreApplication>
#include <cstdlib>
#include <linkedList.h>
#include <iostream>
#include <string>
#include <booking.h>
using namespace std;
int main(int argc, char *argv[]){
QCoreApplication a(argc, argv);
LinkedList<Booking*> allBookings;
Booking* booking1 = new Booking(12);
allBookings.insertNode(booking1);
long test = allBookings.getNode()->getBookingID();
cout << "test: " << test << endl; //doesn't print anything
return a.exec();
}
我没有收到任何错误。cout << "test: " << test << endl;
在 main.cpp 中没有执行,因为我期待它打印一些东西。我在这里做错了什么?我做了一些调试,我认为在我的 Node.h 实现中,当我尝试在 main.cpp 中插入节点时,函数 SetPreviousNode 不会执行。
解决方案
这段代码在insertNode
if (root == nullptr) {
root = node;
node->SetNextNode(end);
end->SetPreviousNode(node);
}else{
end
的值是nullptr
at when you reachend->SetPreviousNode(node);
这可能会使您的程序崩溃。(您应该能够在调试器中确认这一点)。
我想提出一个修复建议,但我不清楚您要做什么。代码看起来有点复杂。另外,我不喜欢在列表中包含光标的设计决定。如果你可以的话,我会放弃它。
推荐阅读
- spring-security - Spring Security OAuth2 客户端:访问 expires_in 值
- unity3d - 在 UWP 的 Unity 插件中使用 DirectX11 的硬件加速 H264 解码
- c# - 基于位置的一致随机数(不基于噪声)
- flutter - Flutter 更新失败
- teiid - 将分页从 OData Translator 扩展到 Web Services Translator
- spring-boot - 如何在 Spring Security 中手动更改身份验证提供程序
- javascript - 如何接受在不同窗口中出现的硒警报?
- android - 在 Xamarin.Forms Android 应用程序中,按下设备后退按钮和应用程序图标后再次调用 MainActivity.OnCreate?
- c - SIGCHLD 和 fork + waitpid() 在一个库中
- javascript - 执行单击,直到可以实际单击元素