
Я пытался решить проблему с помощью Copilot AI, но это не помогло не решишь мою проблему, он предоставил операторы отладки throw.
Код моего класса связанного списка:
Код: Выделить всё
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "Services.h"
#include "Employee.h"
#include "transaction.h"
#include "Citizen.h"
#include
using namespace std;
template
class Node {
public:
T data;
Node* next;
Node* prev;
Node(T val) : data(val), next(nullptr), prev(nullptr) {}
};
template
class LinkedList {
private:
Node* head;
Node* tail;
public:
LinkedList() : head(nullptr), tail(nullptr) {}
Node* getHead() { return head; }
Node* getTail() { return tail; }
void addHead(T val) {
Node* newNode = new Node(val);
if (newNode == nullptr) {
throw std::runtime_error("Memory allocation failed for newNode.");
}
if (head == nullptr) {
head = tail = newNode;
} else {
newNode->next = head;
head->prev = newNode;
head = newNode;
}
// Debugging statements to ensure head and tail are not nullptr
if (head == nullptr || tail == nullptr) {
throw std::runtime_error("Head or Tail is nullptr after adding a new head.");
}
if (head->next != nullptr && head->next->prev != head) {
throw std::runtime_error("Incorrect previous pointer assignment in the linked list.");
}
}
};
#endif // LINKEDLIST_H
Подробнее здесь: https://stackoverflow.com/questions/793 ... ption-in-c
Мобильная версия