To practice using pointers in c++ (coming from experience of c#), I decided to try and write my own linked list class. However using classes and objects seems to do some wacky behaviour that I do not understand.
class Node
{
public:
int value;
Node* Next = NULL;
Node(int Value)
{
this->value = Value;
this->Next = NULL;
}
};
class LinkedList
{
public:
Node* Head = NULL;
Node* Current = NULL;
int count = 0;
LinkedList()
{
Node n(-1);
Head, Current = &n;
}
void pushNode(int value)
{
//Did try stuff here, but display() caused issues.
}
void display()
{
Node* curr = Head;
cout << "HEAD->";
while(curr->Next != NULL)
{
curr = curr->Next;
cout << curr->value << "->";
}
cout << "NULL";
}
};
int main() {
LinkedList ml;
ml.pushNode(10);
ml.display();
return 0;
}
Upon running this code "HEAD->" is written to the console, and then the program ends. If I added an item to the list the console would be spammed with whatever the value was with no end. HELP!