I am trying to print the data of a node from a double linked list using all shared_ptr, but outputting the data with the print function causes the program to crash. I don't know why it crashes when I try to print it. Could there be something wrong with my Insert function?
template <typename T>
class Node
{
private:
T data;
shared_ptr<Node<T>> next;
shared_ptr<Node<T>> prev;
public:
Node() { next = NULL; prev = NULL; data = 0; }
Node(T value) { next = NULL; prev = NULL; data = value; }
T getData() { return data; }
shared_ptr<Node<T>> getNext() { return next; }
shared_ptr<Node<T>> getPrev() { return prev; }
};
template <typename T>
class DoubleLinkedList
{
private:
shared_ptr<Node<T>> head;
shared_ptr<Node<T>> tail;
public:
DoubleLinkedList()
{
head = NULL;
tail = NULL;
total = 100;
for (int i = 0; i < total; i++)
{
Insert(objectgetsinsertedhere);
}
}
void print()
{
shared_ptr<Node<T>> temp;
temp = tail;
temp = temp->getPrev();
cout << temp->getData().getName(); // getName() is a getter to get the name of the object
}
void Insert(T data)
{
T value(data);
if (head == NULL)
{
head = make_shared<Node<T>>(data);
tail = head;
}
else
{
shared_ptr<Node<T>> nu = make_shared<Node<T>>(data);
nu->getPrev() = tail;
if (head == tail)
head->getNext() = nu;
tail->getNext() = nu;
tail = nu;
}
}
};