Traversing the Linked List in C++

Written by Ali on. Posted in C++

Hello there people. For the time we had been discussing the Linked Lists and as in every new post, we get to learn a bit more. In this post, our focus would be on learning how to traverse the Linked Lists and what are the don’ts when traversing the Linked List.

Traversing here in the C++ terminology means going through the Linked List. The nodes in Linked Lists are full of information and for that information to be displayed we have to move very specifically and carefully amongst the nodes. Care has to be taken when choosing the appropriate method to traverse. As the Head pointer contains the starting address of the List, you have to first make a temporary pointer of the type of the Linked Lists’ struct and copy Head’s address in it. The reason we cannot use Head pointer directly to traverse the Linked Lists is because if we do so, we would lose all the nodes while moving ahead the list, as the address in Head would be made to point to the Next pointer of the node. Let us explain that by looking at the following piece of code.

<p>struct student</p>
<p>. string name;</p>
<p>. int rollnum;</p>
<p>. student * Next;</p>
<p>//Assume the Liked List has been created and the information has been added.</p>
<p>int main()</p>
<p>. student * Head;</p>
<p>. //The process to input the information in the Linked List has been skipped</p>
<p>. //assume all entry has been done and is successful</p>
<p>. int count = 0;</p>
<p>. student * Current = Head;</p>
<p>. while(Current != Null)</p>
<p>. {</p>
<p>. cout &lt;&lt; “Student No. ” &lt;&lt; ++count;</p>
<p>.  cout &lt;&lt; Current -&gt; name;</p>
<p>. cout &lt;&lt; Current -&gt; rollnum;</p>
<p>. Current = Current -&gt; Next;</p>
<p>. }</p>
<p>. return 0;</p>

In the above code, after the successful insertion of information in the Linked List, we want to output the information to the console to see whether we have done it correctly or not. For that, we traverse our Lined List. First step is the creation of a temporary pointer of type student named Current and assigning it the value of Head. After that we set the while condition as ‘Current != Null’. This has been done as the Current would be set to The Next of that Current. That actually makes the Current pointer move ahead to the Next pointer of the Node, i.e. move to the next node. For the loop to stop, it will be when the Current pointer of the node is set equal to the Next pointer of the last node; that sets the value of Current to be equal to Next which in turn would be equal to null as being the Next of the last node, and thus the loop will break. For every value of Current, you can output all the information present in the node at which the Current is currently pointing to.

There is one more way of traversing the Linked List by way of using another loop. Figure that out. I will see your response and post that way of traversing the Linked list as a response to this post.

That’s all for now folks. Figure out the way and stay safe!

Tags: ,