0
How to print the linked list elements?
Using while loop
22 Antworten
+ 6
You may find answer in:-
1) https://www.google.com/search?q=how+to+print+linked+list+elements+in+C%2B%2B&rlz=1C9BKJA_enUS705US705&oq=how+to+print+linked+list+elements+in+C%2B%2B&aqs=chrome..69i57j0.37777j0j4&hl=en-US&sourceid=chrome-mobile&ie=UTF-8
2) https://www.geeksforgeeks.org/print-nodes-of-linked-list-at-given-indexes/
3) https://www.codeproject.com/Questions/847388/How-I-Do-Print-Element-Linked-List-Cplusplus
4) http://www.cplusplus.com/forum/beginner/179396/
+ 5
Rohit Kadayan
First Show your Attempt Code Here.
+ 5
Rohit Kadayan Check Now
+ 4
Rohit Kadayan You not have Called the display();
+ 4
Use NULL instead of 0
https://code.sololearn.com/cVKmmZQvhq3l/?ref=app
+ 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;
}
+ 1
Giving no output as result
please suggest any answer and debug please
+ 1
Even after calling display,no output
+ 1
But it is not printing 4
+ 1
Please debugg this please
+ 1
Thanks for help 👏
+ 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.
+ 1
Yes can you please explain it please
+ 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.
0
What you did in this
0
What if we want to insert a node in the beginning
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;
}
0
Thanks