0

This linked list code isn't printing the data; please help

#include<iostream> using namespace std; struct node{ public: int data; struct node *next; }; void print(node *temp,node *head) { temp = head; while(temp != NULL) { cout<<temp->data; temp = temp->next; } } int main() { node *head; node *current; node *temp; int logic; cin>>logic; head = 0; while(logic) { current = new node(); cin>>current->data; if(head == NULL) { head = current = temp; } else { temp->next = current; temp = current; } cout<<"Do you want to continue"; cin>>logic; } print(head,temp); return 0; }

16th Jul 2021, 12:23 PM
LN Shrivas
6 odpowiedzi
+ 1
How does that assign the address of current to temp and head both ? Won't it assign it to just head ? And temp will again make it null . And so your code never touches else loop.
16th Jul 2021, 2:17 PM
Abhay
Abhay - avatar
+ 1
why didn't you included all the programming languages in tags ?
16th Jul 2021, 12:32 PM
Abhay
Abhay - avatar
+ 1
What do you think head=current=temp is doing ?
16th Jul 2021, 1:44 PM
Abhay
Abhay - avatar
+ 1
Abhay I'm assigning the address of newly created node that is current to head and temp both
16th Jul 2021, 2:14 PM
LN Shrivas
+ 1
Abhay thanks man it's working now !!
16th Jul 2021, 4:31 PM
LN Shrivas