简体   繁体   中英

Linked list: Difference between “node* head=new node” and “node* head”

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;
}

Both your cases are undefined.

In your former case,

node* head=new node

head points to a new node whose next is uninitialized. This would mean head->next = <Garbage> .

       _________
      |DATA|NEXT|
head->|----|----|
      |Junk|Junk|      
      |____|____|

After 1st Insert

       _________    _________
      |DATA|NEXT|  |DATA|NEXT|
head->|----|----|  |----|----|
      |  0 |----|->|Junk|Junk|      
      |____|____|  |____|____|


After nth Insert

       _________      _________    _________
      |DATA|NEXT|    |DATA|NEXT|  |DATA|NEXT|
head->|----|----|    |----|----|  |----|----|
      |  n |----|...>| 0  |----|->|Junk|Junk|      
      |____|____|    |____|____|  |____|____|

In your second case

node* head;

head now points to some undefined value.

head->Junk

After nth Insert

       _________      _________   
      |DATA|NEXT|    |DATA|NEXT|  
head->|----|----|    |----|----|  
      |  n |----|...>| 0  |----|->Junk
      |____|____|    |____|____|  

Both the cases, are similar, except that in the former cases, it just iterates to a blank node, after which the behavior becomes undefined.

Second case, is similar to the former except there is no blank (uninitialized) node.

Solution

node* head=nullptr;

Declare and initialize your head to nullptr

head->nullptr

After nth Insert

       _________      _________   
      |DATA|NEXT|    |DATA|NEXT|  
head->|----|----|    |----|----|  
      |  n |----|...>| 0  |----|->nullptr
      |____|____|    |____|____|  

This is because your code is invoking undefined behavior when you access a pointer that doesn't point to a valid memory. When you invoke undefined behavior, anything can happen, including your code working correctly.

If you don't set the pointer at the start, then its value is undefined.

In your former case, your accessing an invalid pointer when you try and read the values inside of first head you create. Because you never set the value of node* next; then it is set to undefined value and when it tries to access the value your program crashes.

An easy fix for this is to add a constructor that sets the value

node() : data(0), next(nullptr){}

Another issue with your code is here

node* temp= new node;
temp=head;

You are unnecessarily creating a node using dynamic memory but you never delete it. Change your code to this

node* temp = head;

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.

If you are creating the head nod with this code -- node* head=new node; --then you have to initialize the initial value of head node to NULL other wise it will take zero(0) ie, head = NULL; as,

    int main()
{
    head = NULL; // <<------Here
    int size,i;
    cout<<"Enter size of linked list"<<endl;
    cin>>size;
    for(i=0;i<size;i++)
    {
    PushFront(i);
}

Just put one extra line in your above code and it runs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM