{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Algo2Notes

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

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

1

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

View Full Document
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

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

View Full Document
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
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

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

View Full Document
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.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern