Linked list: Difference between "node* head=new node" and "node* head"
c++
Solution
If you just declare `node* head`, then the value of `head` is undefined ("junk") and you should refrain from using it.
An additional problem in your code is at:
node* temp= new node;
temp=head;
Not sure what you're trying to do by setting `temp = something` and then `temp = something else`. Obviously, the first assignment is useless, because it is overridden by the second assignment. And in this specific case, it leads your program to memory leaks, as you "lose" the dynamically-allocated memory pointer.
Problem
I am creating a link list of size n entered by user.Here when I just initialize the header the output is perfect but when I declare it as well output has two zeroes appended. For size=5 If I write node* head=new node; output is 432100 and if I write just node* head output is 43210. Why is that? ``` /* I am creating a link list of size n entered by user * File: main.cpp * Author: neha * * Created on February 2, 2014, 12:39 AM */ #include <cstdlib> #include <string> #include <sstream> #include <iostream> using namespace std; /* * */ using namespace std; struct node{ int data; node* next; }; node* head=new node; //<--------------here void PushFront(int value) //Inserting nodes at front of link list { node* newNode= new node; newNode->data=value; newNode->next=head; head=newNode; } void Print() //printing the inserted nodes { node* temp= new node; temp=head; while(temp!=NULL) { cout<< temp->data; temp=temp->next; } } int main() { int size,i; cout<<"Enter size of linked list"<<endl;//Asking user to enter the size of linklist cin>>size; for(i=0;i<size;i++) { PushFront(i); } Print(); return 0; } ```