I have a problem with this code. I'm fairly new to C++ however most of it is already easy to understand. I have tried making a simple linked list data structure, however, it prints garbage and not the values inside the list. My question is, where did I go wrong in my syntax to have it display the addresses?
Output:
class Node
{
public:
int data;
Node *next;
Node(int data)
{
data = data;
next = NULL;
};
};
class LinkedList
{
Node *first;
Node *last;
int count;
public:
LinkedList()//constructor for the LinkedList
{
//initialization
first = NULL;
last = NULL;
count = 0;
};
void AddItem(int data)
{
Node *newItem = new Node(data);
if(first == NULL)
{
first = newItem;
last = newItem;
}
else
{
Node *traversal = first;
while(traversal->next != NULL)
{
traversal = traversal->next;
}
traversal->next = newItem;
last = traversal->next;
}
count++;
}
void DisplayList()
{
cout<<endl;
Node *traversal = first;
while(traversal->next != NULL)
{
cout<<"["<<traversal->data<<"] ";
traversal = traversal->next;
if(traversal == NULL)
{
break;
}
}
}
bool isEmpty()
{
if(count < 1)
{
cout<<"List is empty";
return true;
}
else
{
cout<<"List is not empty";
return false;
}
}
};
int main()
{
cout <<"Linked Lists demo"<<endl;
LinkedList collection;
collection.AddItem(1);
collection.AddItem(3);
collection.AddItem(5);
collection.AddItem(7);
collection.AddItem(9);
collection.AddItem(11);
collection.isEmpty();
collection.DisplayList();
cin.get();