When it comes to linked list in C++ how do you transverse it? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

When it comes to linked list in C++ how do you transverse it?

Linked Lists

31st Jan 2017, 12:32 PM
Karl Cruz
Karl Cruz - avatar
18 Answers
+ 5
the following code will help you in pop approach struct node { int data; node* next; }; node *start,*end; void traversal() node *t; while (t!=NULL) { cout<<t->data; t=t->next; } }
1st Feb 2017, 8:52 AM
Wazed Ali Gazi
Wazed Ali Gazi - avatar
+ 4
#include <list> std::list<int> your_list; /* you can remove std:: if you are using nespace STD and you can change int to any type your_list.push(element); to push elements into the list (if push doesn'work try with push_back)*/ //to print all the element in the list you can do like this: for (std::list<int>::iterator it=tour_list.begin(); it < your_list.end(); ++it) { std::cout << *it << std::endl; } // or with c++11 syntax for (auto var : your_list) { std::cout << var << std::endl; }
6th Feb 2017, 6:46 AM
AleC
AleC - avatar
+ 1
Well just give the start pointer address to a pointer and then check if it is null then exit otherwise write pointerName =pointerName->link . repeat this until ptr is not null for example node *ptr = start; while(ptr != nullptr) /*You can use Null in place of nullptr*/ { //do any operation ptr = ptr -> link; }
20th Feb 2017, 2:45 PM
Mohit Sahu
Mohit Sahu - avatar
+ 1
I have a cpp_timestamp code in my codes if you can see them. I made it out of a template stack linked list. there isnt a print function but there is a makeEmpty. you would interate it in a similar way. go check it out!!
24th Feb 2017, 8:27 AM
Michael Szczepanski
Michael Szczepanski - avatar
+ 1
class node { int data; node *head; node *next; void traverselist() { node *temp; temp=head: while(temp!=NULL) { cout<<temp->data; temp=temp->next; } } }; void main() { node obj; obj.traverselist(); }
9th Mar 2017, 8:31 PM
Muhammad Moiz
Muhammad Moiz - avatar
- 1
inserting data and then printing it as above code is traversing
1st Feb 2017, 10:37 AM
chitra gautam
chitra gautam - avatar
- 1
Traversing a list basically means Processing each element of the list. You can print the elements or double the value of each element etc.
1st Feb 2017, 6:54 PM
Nitesh Lekhi
Nitesh Lekhi - avatar
- 1
just use the pointer reference and ->() corresponding item in the list
3rd Feb 2017, 6:01 PM
MAYUR PAREKH
- 1
you'd better use stl::list in C++. #include<list> using namespace std; list<type> mylist; // makes a linkedlist of type
5th Feb 2017, 8:14 PM
Edwin Martens
Edwin Martens - avatar
- 1
what
13th Mar 2017, 8:03 PM
jose ignacio
- 2
i love c++😍😍😍
16th Feb 2017, 2:11 AM
محمد عبد الرزاق
محمد عبد الرزاق - avatar
- 2
\n
17th Feb 2017, 12:19 PM
Shiv Raj Chaudhary
- 2
هلا
20th Feb 2017, 12:51 PM
كنق يا عيال حبتين
كنق يا عيال حبتين - avatar
- 2
put start->head. put temp->start. while (temp->next!=null) temp=temp->next. that's it!
25th Feb 2017, 7:38 AM
Neeraj Roy
Neeraj Roy - avatar
- 5
[]}
26th Feb 2017, 6:19 PM
maromi
maromi - avatar
- 5
kunss rejew avri
26th Feb 2017, 6:19 PM
maromi
maromi - avatar
- 8
Set caff e dag 3 sg b dat 65 CLM dy se! We'd e e ml ever f tyd k . H=secede 2 set were se sc
2nd Feb 2017, 7:43 PM
Renier
- 8
Set caff e dag 3 sg b dat 65 CLM dy se! We'd e e ml ever f tyd k . H=secede 2 set were se sc
2nd Feb 2017, 7:43 PM
Renier