Traverse Linked List in C++


Prev Tutorial Next Tutorial

Traverse Linked List in C++

When we want to see the information stored inside the linked list. We create node*temp. Transfer the address of *head to *temp. So *temp is also pointed at the front of the linked list. Linked list has 3 nodes. We can get the data from first node using temp->data . To get data from second node, we shift *temp to the second node. Now we can get the data from second node.

traverse linked list in c++

Syntax

while( temp!=NULL )
{
 cout<< temp->>data<<" ";  // show the data in the linked list
 temp = temp->next;  // tranfer the address of 'temp->next' to 'temp'
}

This process will run until the linked list's next is NULL.


Prev Tutorial Next Tutorial

Advertisements




Download Projects