Output is not printing linked list

/******************************************************************************

                          Online C++ Compiler.
           Code, Compile, Run and Debug C++ program online.

Write your code in this editor and press “Run” button to compile and execute it.

*******************************************************************************/

#include
using namespace std;

struct node{
int data;
struct nodenext;
};
struct node
head=NULL,*newnode,temp;
int main(){
int choice=1;
while(choice)
{
newnode=(struct node
)malloc(sizeof(struct node));
cin>>newnode->data;
newnode->next=0;
if(head==0)
{
newnode=head=temp;
}
else
{
newnode->next=head;
head=newnode;
}
cout<<“do you want to continue”;
cin>>choice;

}
temp=head;
while(temp!=0)
{
cout<data;
temp=temp->next;
}

}

Hi @supratik260699
Your code is not printing any output because in while loop in if case you are using newnode=head=temp; due to which newnode and head is given value of temp , hence head becomes null every time and linked list is not formed. Instead you should use head=temp=newnode;

1 Like