+ 4
When it comes to linked list in C++ how do you transverse it?
Linked Lists
18 Respostas
+ 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;
}
}
+ 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;
}
+ 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;
}
+ 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!!
+ 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();
}
- 1
inserting data and then printing it as above code is traversing
- 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.
- 1
just use the pointer reference and ->() corresponding item in the list
- 1
you'd better use stl::list in C++.
#include<list>
using namespace std;
list<type> mylist; // makes a linkedlist of type
- 1
what
- 2
i love c++😍😍😍
- 2
\n
- 2
هلا
- 2
put start->head.
put temp->start.
while (temp->next!=null)
temp=temp->next.
that's it!
- 5
[]}
- 5
kunss rejew avri
- 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
- 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