Data Structures and Algorithms with Object-Oriented Design Patterns in Python
next up previous index

Positions of Items in a List

As shown in Program gif, objects that implement the Cursor interface can be used to access, insert, and delete objects in an ordered list. Program gif defines the nested class called OrderedListAsArray.Cursor that extends the abstract Cursor class. The idea is that instances of this class are used by the OrderedListAsArray class to represent the abstraction of a position in an ordered list.

   program8767
Program: OrderedListAsArray.Cursor class __init__ and getDatum methods.

The Cursor class has two instance attributes, _list and _offset. The _list instance attribute refers to an OrderedListAsArray instance and the _offset instance attribute records an offset in that list's array of objects. The __init__ method simply assigns the given values to the _list and _offset instance attributes. Program gif also defines the getDatum method of the Cursor class. This method returns the item in the array at the position record in the _offset instance attribute, provided that position is valid. The running time of the accessor is simply O(1).


next up previous index

Bruno Copyright © 2003, 2004 by Bruno R. Preiss, P.Eng. All rights reserved.