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 index. index 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: