Algo2Notes

Algo2Notes - 1 The ability to traverse the data structure...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The ability to traverse the data structure is an essential component of almost any algorithm. You have to be able to get to the data in order to process it. Whereas in the case of arrays traversing the data structure is straight forward and it is achieved with one or several nested for loops, in the case of irregular data structures traversals have to follow the more complex topology of the data structure. Let’s first have a look at traversing a linked list. Once we’ll know how to traverse a linked list we’ll have an excellent start for devising algorithms that achieve more complex data processing. 2
Background image of page 2
Here is the basic linked list traversal algorithm. In this example the linked list nodes store a transaction value. The traversal algorithm prints out all transaction values. The input to the algorithm is the linked list, specified with the link to the first node. In addition to the payload, i.e. the transaction value, a node also stores a link to the next node. The output of the algorithm is the printout of all transaction values. The algorithm is called TraverseList. The variable curr is a link to the current node. Initially, curr is a link to the first node, i.e. L. Then the list is traversed with a while loop which keep going “while” the link curr is not null. The notation “!=“ means not equal. The body of the while loop prints out the value of the current node and moves to the next node. Moving to the next node is done by assigning to curr the link to the next node. 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The notation “LINK”->”FIELD” gives access to the field called “FIELD” of the node pointed to by the link “LINK”. For the last element, the next link is NULL. This sets curr to NULL, the condition curr != NULL is false, and the while loop stops. 3
Background image of page 4
Initially, curr is set to L. Curr is not NULL, thus the while loop is executed the first time. The value of curr, which is 13.40, is printed out, and curr is set to the link to the next element, i.e. the link to the second element. We reach the end of the body of the while loop, so we have to go check the condition to see if we need to execute the while loop one more time. Curr is not NULL, thus the while loop is executed for the second time. We print out 12.50, set curr to the link to the next element, and check the while loop condition again. Curr is not NULL , thus the while loop is executed for the third time. We print out 7.45, we set curr to the link to the next element, and check the while loop condition again. Curr is still not NULL, thus the while loop is executed for the fourth time. We print out 2.50, we set curr to the link to the next element which is NULL, and check the while loop condition again. 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/27/2012 for the course CS 177 taught by Professor Staff during the Spring '08 term at Purdue.

Page1 / 69

Algo2Notes - 1 The ability to traverse the data structure...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online