0

this is my code for inserting a node at the head of the linked list. there is no error but my printllist function is not working I don't know why. I think I have done silly somewhere. help me out I am stuck here for 3 hours.

#include <iostream>
#include <bits/stdc++.h>

using namespace std;
  class Node
   {   public:
       int data;
       Node* next;
   };



void insert(int y,Node* head){
  
   Node *temp=new Node();
   temp->data=y;
   temp->next=head;
   head=temp;

}

void printllist(Node *head){

   if(head=NULL)
   {
      cout<<"list is empty"<<endl;
   }
   else
   {
      while(head!=NULL)
      {
         cout<<head->data<<" ";
         head=head->next;
      }
   }

}

int main(){

   Node *head;
   head=NULL;
   int x,y,z;

   cout<<"hey can you tell me how many number you want to insert"<<endl;
   cin>>x;
   for(int i=0;i<x;i++)
   {
      cout<<"enter the data that you want to insert"<<endl;
      cin>>y;
      insert(y,head);
      printllist(head);
   }

   return 0;
}
Turo
  • 4,724
  • 2
  • 14
  • 27
vikash
  • 27
  • 5
  • 3
    The changes to `head` don’t reflect outside `insert` so it will stay `NULL`. You need a reference to a pointer – Sami Kuhmonen Apr 01 '21 at 08:04
  • what does `z` supposed to be? – leech Apr 01 '21 at 08:05
  • i am a newbie can you elaborate little more please – vikash Apr 01 '21 at 08:06
  • @leech it nothing actually just forgot to remove – vikash Apr 01 '21 at 08:07
  • 1
    @vikash 1) SO is not tutorial site. To learn C++, consider learning from [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). 2) [What's the difference between passing by reference vs. passing by value?](https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value) – Algirdas Preidžius Apr 01 '21 at 08:08
  • 1
    Also, since you are using C++ and C++ is a object-oriented language, you should put your fuctions in your class as methods. – leech Apr 01 '21 at 08:10
  • 1
    Here, you should either return `head` from `insert`, or pass a `Node **head`. A parameter is a local variable of the function. Changing it does not change the caller variable. – Serge Ballesta Apr 01 '21 at 08:11
  • Does this answer your question? [Linked list for stack, head->next keeps becoming null](https://stackoverflow.com/questions/28498069/linked-list-for-stack-head-next-keeps-becoming-null) – Yunnosch Apr 01 '21 at 08:17
  • Admittedly it is C, but it is the same problem. – Yunnosch Apr 01 '21 at 08:17

1 Answers1

1

You need to pass the head of the linked list by reference so that the changes which are made in the insert function actually remain preserved between the function calls. Also, keep in mind that all the new nodes are inserted at the beginning of the linked list. So, for example if you enter 1,2,3 as the three elements of the linked list, the print function will print 3 2 1.

Try this:

#include <iostream>
#include <bits/stdc++.h>

using namespace std;
  class Node
   {   public:
       int data;
        Node* next;
   };
void insert(int y,Node** head){
  
Node* temp = new Node();
    temp->data = y; 
    temp->next = *head; 
    *head = temp;
}
void printllist(Node* head)
{
    if(head==NULL)
        cout<<"List is empty";
    while(head!=NULL)
    {
       cout<<head->data<<" ";
       head=head->next;
    }
}

int main(){

Node *head;
head=NULL;
int x,y,z;
  cout<<"hey can you tell me how many number you want to insert"<<endl;
  cin>>x;
  for(int i=0;i<x;i++)
  {   cout<<"enter the data that you want to insert"<<endl;
 cin>>y;
 insert(y,&head);
}
printllist(head);

return 0;
}
Suyash Krishna
  • 241
  • 2
  • 8