首页 > 解决方案 > 为什么我的模板链接列表不起作用?

问题描述

我无法指定问题所在,也没有收到任何错误。这是我的代码:

节点.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 不会执行。

标签: c++templateslinked-list

解决方案


这段代码在insertNode

if (root == nullptr) {
    root = node;
    node->SetNextNode(end);
    end->SetPreviousNode(node);
}else{

end的值是nullptrat when you reachend->SetPreviousNode(node);这可能会使您的程序崩溃。(您应该能够在调试器中确认这一点)。

我想提出一个修复建议,但我不清楚您要做什么。代码看起来有点复杂。另外,我不喜欢在列表中包含光标的设计决定。如果你可以的话,我会放弃它。


推荐阅读