Hi guys
I have been studying linked list tutorial. I want to add a new node to the begining of my linked list but I am making a mistake.
Can anyone show me what is wrong with my code?
Code:
#include <iostream>
using namespace std;
class node
{
public:
int number;
node* next;
};
int main()
{
node* root; //head point to first node
node* conductor; //pointer that moves node by node
root=new node; //dynamicly creating a new node and point to head
root->next=0;
root->number=5; //first number in my list
conductor=root; //set conductor to root
if (conductor!=0) //check if there is a node in the list
{
while(conductor->next!=0) //go to the end of the list
conductor=conductor->next;
}
conductor->next=new node; //create a new node
conductor=conductor->next; //set conductor to new node
conductor->number=10; //set seconds node number to 10
conductor->next=0; //set node next pointer to the NULL
conductor->next=new node; //create third node to put it the beginning of the list
conductor->number=1; //set third number to 1
conductor->next=root; //set the next part of the new node equal to root, which makes it point to the first node in the list ????
conductor=root; //set root to point to the new node ???
if (conductor->next!=0) //print list
{
while(conductor->next!=0)
{
cout<<conductor->number<<' ';
conductor=conductor->next;
}
cout<<conductor->number;
}
cout<<endl;
Output should be like this
1 5 10
Thanks