Attempted to build a doubly linked list and printing it out. However, I received a linker command failed after adding "static Node* lastAdded". Not sure what the reason is.
Also, for the head node, I would like leave "int data" uninitialized. Is there a better way to leaves data uninitialized than what I have below?
#include <iostream>
#include <string>
using namespace std;
struct Node {
static Node* lastAdded;
Node(const int data); // General data node constructor
Node(); // head constructor
static void push(Node* previousNode);
int data;
Node* previous;
Node* next;
};
Node::Node(const int data) {
this->data = data;
}
Node::Node() {
// Note that data is left uninitalized for head node
previous = nullptr;
next = nullptr;
lastAdded = this;
}
static void push(Node* currentNode, Node* previousNode) {
previousNode->next = currentNode;
currentNode->previous = previousNode;
currentNode->next = nullptr;
Node::lastAdded = currentNode;
}
int main()
{
Node* head = new Node();
push(new Node(1), Node::lastAdded);
push(new Node(12), Node::lastAdded);
for (Node* temp = head; temp->next != nullptr; temp++) {
if (temp->previous == nullptr)
temp++;
cout << temp->data << endl;
}
}