{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
1
Image of page 1

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

View Full Document Right 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
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
Image of page 3

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

View Full Document Right 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
Image of page 4
Here is a trace of the algorithm on the linked list shown. 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
Image of page 5

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

View Full Document Right Arrow Icon
Curr is NULL, thus curr != NULL is false, so the while loop is not executed anymore and we reach the end of the algorithm marked by endTraverseList.
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern