[   ^ to index...   |   next -->   ]

CSE 143/AE : 6 July 2000


ADT example: Vector

Conceptually, a vector is a list of elements which can be accessed by index.

class Item { public: Item(); bool equals(Item& other); bool lessThan(Item& other); private: // some stuff }; const int MAX_VECTOR_SIZE = 40; class Vector { public: Vector(); bool isEmpty(); int length(); void insert(int position, Item item); Item delete(int position); Item get(int position); // Find the index of the given item; // return -1 if not in vector int indexOf(Item item); private: Item contents[MAX_VECTOR_SIZE]; int size; };

Exercises (solutions...)

  1. What should the constructor of this class do?
  2. Implement the indexOf method, using only other public methods of Vector (do not access the private members).
  3. What if Vector kept all its Items in sorted order? Implement indexOf for a sorted vector.

Last modified: Thu Jul 6 13:56:34 PDT 2000