Traversing the List

Woman Reading Long List

Last week, we covered the basics of the lists in Computer Science. A common operation on lists is traversal of the list. Traversal is the process of accessing each element of a list in order and performing some operation. Let’s see how we can do it in SNAP!.

While accessing each element of the list, we need a pointer to keep track of the current element we are accessing in the list. To achieve that, we will use a variable, let’s call it indexindex will start at the first element of the list, and move to the last element of the list one element at a time. After starting from the first element, to move the index to the next element, we will need to increment it by 1.

Let’s see this in action:

Traversal of List in SNAP!Copy this code in your Scripting area and see the results for yourself. Then, change the input of list and see how it impacts the output.

Share This:

Leave a Reply

Your email address will not be published. Required fields are marked *