Nikhil Chilwant
Nikhil Chilwant

Reputation: 649

Printing Linked List in C++

My following code print just only first element. In print_list() function, it stops after printing first element. It says after first element, head->next is 0. Shouldn't point towards second element?

I want to simply print whole list.

#include<iostream>
#include<cstdlib>
using namespace std;    
struct node {
  int x;
  node *next;
};    
node* add_element(node*);
bool is_empty(node*);
void print_list(node*);
node* search(node*);

int main()
{
    node *head;
    head=NULL;
    node* current=head;
    for(int i=0;i<5;i=i+1)
    {
       if (current==NULL)
       { 
         current=add_element(current);
         head=current;
       }
       else{ 
          current=add_element(current);
       }
     }
     cout<<head->next<<endl;

     // DOUBT: head->next gives NULL value. It should give me pointer to 2nd node
     print_list(head);
}     
node* add_element(node* current)
{   
    node* temp;
    temp=new node;
    temp->next=NULL;
    cout<<"enter element"<<endl;
    cin>>temp->x;
    current=temp;
    return current; 
}      
bool is_empty(node* temp)
{
    return temp==NULL;      
}    
void print_list(node* temp)
{
    if (is_empty(temp)==false)
    {   
         cout<<"here temp(head)"<<temp->next<<endl;
         while(temp!=NULL)
         {
            cout<<temp->x<<endl;
            temp = temp->next;
         }
    }
}

Upvotes: 0

Views: 21186

Answers (4)

Grijesh Chauhan
Grijesh Chauhan

Reputation: 58261

Print function print first element because you have just one node in the Linked List! Actually the mistake is present in add_element(node*) function, you overwrite address of head node with new node (so having memory leak) as I marked below:

  node* add_element(node* current)
  {   
    node* temp;         
    temp = new node;     <---" You allocated memory"
    temp->next = NULL;   <---" Set next NULL"
    cout<< "enter element" << endl;
    cin>> temp->x;       <---" Assign a value in new node"    

    // Replace below two line with suggested   
    current = temp;      <---"MISTAKE: Overwrite first node"    
                            "temp next is NULL so losing address of other nodes"

    return current;      <--- "return first node"
  }

Next of new node (so first node) is NULL hence the print function prints only first node's value.

Suggestion:

You should Correct as follows to add new node as a first node in linked list:

temp -> next = current;  // new nodes next if present first node
return temp;             // new code becomes first node

Be careful current should be NULL initially.

With my suggestion in add_element() function also change the for loop code in main() as follows:

for(int i=0; i < 5; i = i + 1){
    current = add_element(current);
}
head = current;

And check the working code at Codepade (instead of user input I added value using y = 100 variable).

Edit To append new node:

You need to check whether new node is first node of not (read comments).

  // returns first node address in linked list = head 
  node* add_element(node* head){
    node *temp, *new_nd;

    // Create new node
    new_nd = new node;
    new_nd->next = NULL;
    cout<<"enter element"<<endl;
    cin>>new_nd->x;

    // Is new node is the first node?
    if(!head)  
      return new_nd;

    // move to last 
    temp = head; 
    while(temp->next) temp = temp->next;

    // add new node at last 
    temp->next = new_nd;

    // return old head
    return head;  
  }

Also simply main() as below:

int main(){
    node *head = NULL;
    for(int i = 0; i < 5; i = i + 1){
        head = add_element(head);
    }
    print_list(head);
}

check this working code.

Upvotes: 1

Karma
Karma

Reputation: 1

if (current==NULL)
                    { current=add_element(current);
                      head=current;
                    }
                    else
                    { current->next=add_element(current);
                      current=current->next;
                    }

The correct code. You have to make a small correction in the loop. You have to add a new node and then make it point to the next of the current node. so the simplified code is current->next=add_element(current) and then make current point to the new current.

Upvotes: 0

arp001
arp001

Reputation: 31

Your problem is here:

node* add_element(node* current)
  {   
    node* temp;   //You created a new node
    temp=new node;  //You allocated it here
    temp->next=NULL;  //You set its next property to null
    cout<<"enter element"<<endl;  //
    cin>>temp->x;
    current=temp;   //This should be current->next = temp. You are overwriting it!
    return current; //And now you are returning essentially the temp object that
                    //You created and you set its next property to NULL
  }

You are assigning the node you created in temp = new node to the current node that was passed in. What you want to do is assign the node you just created to the current node's next property. It should be current->next = temp

Upvotes: 1

Michał Wr&#243;bel
Michał Wr&#243;bel

Reputation: 684

head->next is NULL because you set it so in add_element(). To have a linked list, you should set current->next = temp.

As you're using C++, you might consider using std::list instead of implementing your own linked list.

Upvotes: 0

Related Questions