I am trying to write a basic link list, so that I can understand how to implement it in C++. The tutorial I read was extremely advanced (giving more code than needed to understand and not explaining really how). So i tried to write this simple linked list:
#include <iostream>
using namespace std;
struct node
{
int data;
node *next;
}*p;
int main()
{
int num;
while(true)
{
node *l = new node;
l->next = 0;
cout<<"Enter numbers (0 to stop): ";
cin>>num;
if(!num)
break;
if(p == NULL)
{
l->data = num;
}
else
{
node *tmp = new node;
tmp->data = l->data;
tmp->next = 0;
l->next = new node;
l->data = tmp->data;
delete tmp;
}
cout<<l->data<<endl;
}
cin.get();
}
But it dosnt work. I think that I might not create then new 'node' correctly...
So can someone please explain to me how implement my code correctly?
Thanks in advance...