I am creating a linked list from scratch. Here is the code. at first, I have defined node *root=NULL;
that means root node has no element. But when I append the first element I have to create root=new node();
is not it that node *root=NULL;
has created a root node? then why I have to use root=new node();
.
actually i am confused among node *root=NULL;
& root=new node();
& root==NULL
'. would you please make me understand?
#include<bits/stdc++.h>
using namespace std;
struct node
{
int roll;
node *next;
};
node *root=NULL;
void append(int roll)
{
if( root==NULL)
{
root=new node();
root->roll=roll;
root->next=NULL;
}
else
{
node *current_node=root;
while(current_node->next!=NULL)
{
current_node=current_node->next;
}
node *newnode=NULL;
newnode=new node();
newnode->roll=roll;
newnode->next=NULL;
current_node->next=newnode;
}
}