0

How to print the linked list elements?

Using while loop

21st May 2020, 2:48 AM
Rohit Kadayan
Rohit Kadayan - avatar
22 Respuestas
21st May 2020, 6:52 AM
ROHIT KANOJIYA
ROHIT KANOJIYA - avatar
+ 5
Rohit Kadayan First Show your Attempt Code Here.
21st May 2020, 3:28 AM
ROHIT KANOJIYA
ROHIT KANOJIYA - avatar
+ 5
Rohit Kadayan Check Now
21st May 2020, 5:26 AM
ROHIT KANOJIYA
ROHIT KANOJIYA - avatar
+ 4
Rohit Kadayan You not have Called the display();
21st May 2020, 5:09 AM
ROHIT KANOJIYA
ROHIT KANOJIYA - avatar
21st May 2020, 5:14 AM
ROHIT KANOJIYA
ROHIT KANOJIYA - avatar
+ 1
#include <iostream> using namespace std; struct node { int data; node* next; }; void display(node* n) { while ((n->next)!=0) { cout<<n->data<<endl; n=n->next; } } int main() { node* head ; node* second; node* third; head=new node ; second=new node; third= new node; head->data=2; head->next=second; second->data=3; second->next=third; third->data=4; third->next=0; return 0; }
21st May 2020, 4:40 AM
Rohit Kadayan
Rohit Kadayan - avatar
+ 1
Giving no output as result please suggest any answer and debug please
21st May 2020, 5:01 AM
Rohit Kadayan
Rohit Kadayan - avatar
+ 1
Even after calling display,no output
21st May 2020, 5:12 AM
Rohit Kadayan
Rohit Kadayan - avatar
+ 1
But it is not printing 4
21st May 2020, 5:15 AM
Rohit Kadayan
Rohit Kadayan - avatar
+ 1
Please debugg this please
21st May 2020, 6:23 AM
Rohit Kadayan
Rohit Kadayan - avatar
+ 1
Thanks for help 👏
21st May 2020, 6:53 AM
Rohit Kadayan
Rohit Kadayan - avatar
+ 1
Here I suppose to print singly linked list. The code is -- #include <iostream> using namespace std; struct Node { int data; struct Node *next; }; struct Node* head = NULL; void insert(int new_data) { struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = head; head = new_node; } void display() { struct Node* ptr; ptr = head; while (ptr != NULL) { cout<< ptr->data <<" "; ptr = ptr->next; } } int main() { insert(3); insert(1); insert(7); insert(2); insert(9); cout<<"The linked list is: "; display(); return 0; } Output : The linked list: 9 2 7 1 3 Now , if you face any error you can reply me i'll try to solve that error.
21st May 2020, 7:52 AM
Shweta Rao
Shweta Rao - avatar
+ 1
Yes can you please explain it please
21st May 2020, 8:06 AM
Rohit Kadayan
Rohit Kadayan - avatar
+ 1
yes Rohit Kadayan In this program The function insert() inserts the data into the beginning of the linked list. It creates a new_node and inserts the number in the data field of the new_node. Then the new_node points to the head. Finally the head is the new_node i.e. the linked list starts from there.  The function display() displays the whole linked list. First ptr points to head. Then it is continuously forwarded to the next node until all the data values of the nodes are printed.   the function main(), first various values are inserted into the linked list by calling insert(). Then the linked list is displayed.
21st May 2020, 9:12 AM
Shweta Rao
Shweta Rao - avatar
0
What you did in this
21st May 2020, 5:32 AM
Rohit Kadayan
Rohit Kadayan - avatar
0
What if we want to insert a node in the beginning
21st May 2020, 6:21 AM
Rohit Kadayan
Rohit Kadayan - avatar
0
#include <iostream> using namespace std; struct node { int data; node* next; }; void display(node* n) { while (n!=0) { cout<<n->data<<endl; n=n->next; } } void insertbrg (int d) { node* newnode; newnode=new node; newnode->data=d; newnode->next =head; head=newnode; } int main() { node* head ; node* second; node* third; head=new node ; second=new node; third= new node; head->data=2; head->next=second; second->data=3; second->next=third; third->data=4; third->next=0; display(head); insertbrg(6); display(head); return 0; }
21st May 2020, 6:22 AM
Rohit Kadayan
Rohit Kadayan - avatar
0
Thanks
21st May 2020, 9:21 AM
Rohit Kadayan
Rohit Kadayan - avatar
21st May 2020, 12:35 PM
Asqar Arslonov
Asqar Arslonov - avatar