Linear Data Structure: Iteration Flashcards
What is a cursor?
An abstraction that represents a position with a collection; a property of a collection that records a specific position
Operations on a cursor interface
itemExists (check)
item (getter)
goFirst (mover in list)
goForth (mover in list)
goLast (mover in list)
goBefore (mover outside of list)
goAfter (mover outside of list)
before (test)
after (test)
Are all operations necessary?
No, different data structures/ADTs will have different types of operations
How to represent: between 2nd and last element in a list
- position value: non-null
- prevPosition value: non-null
- # elements in list: > 1
How to represent: first element in a list
- position value: non-null
- prevPosition value: null
- # elements in list: > 0
How to represent: after in a list
- position value: null
- prevPosition value: non-null
- # elements in list: > 0
How to represent: before in a list
- position value: null
- prevPosition value: null
- # elements in list: > 0
How to represent: before and after in a list
- position value: null
- prevPosition value: null
- # elements in list: 0
What are the external differences between the cursor interface of the ArrayedList and LinkedList classes
None, they are the same, which is an advantage of cursors; cursors have the same outward functionality regardless of the different list structure
Are there internal differences between the cursor interface of the ArrayedList and LinkedList classes
Yes, different data structures have different internal implementations, even though they externally work the same
What is an iterator? How is it different from a cursor
A distinct object that represents a position in an instance of a collection and always has public methods for iteration.
Cursors are just an abstraction of a position and may not have public methods
What is the idea behind iterators?
- An instance method of a list (or any collection) is called that returns an iterator object.
- The iterator object implements the cursor interface.
- The iterator object can be used to “loop” over the elements of the list instance which generated it.
What’s the purpose of iterators?
Cursors can be used for something else in the moment, while we are iterating with an iterator.
Otherwise, it would be up to the user to save and restore the collection’s cursor before and after using the cursor to iterate