computer science II
c m s c 214  
f a l l   2 0 0 1  

An Abstract List

When you're implementing a class, it's very easy to get caught up with the implementation. For example, you might be writing a class that uses a dynamic array or a linked list. However, try to remember that often, you are writing this class (in principle) for other programmers to use.

Very often, you are trying to create some mathematical entity. These entities are considered "abstract", because they aren't implemented. For example, when you think of a set, you think of something that looks like {1, 3, 5, 10}.

You may know something about sets already. For example, no elements are repeated, and sets are unordered.

When you implement a set, however, you have to think about whether it should be implemented using a dynamic or static array, or whether you should use some sort of linked list. It's easy to get so concerned about linked lists and arrays that you forget that the person using your class just doesn't care. All they want to do is to use a Set.

Thus, you have to provide methods that will get them to think of your class as a set. This is very similar to designing, say, a television. As a designer, you always keep in mind that the user only worries about getting a good quality picture, and wants some basic operations. They don't ever want to think about how the TV is constructed. They don't want to know about the electronics or physics that make televisions possible.

As you implement a doubly linked list, think about what you are presenting to the user. You want the user (who is really a programmer using your class) to think of this as a sorted list.

A sorted list may seem like an obvious concept (especially if it's a list of integers), but there are some subtleties.

  1. A list is sorted by a "key". This may not be so obvious, especially is it's a list of integers (the integer is the key). However, if you have a list of Students, you might sort based on the last name, student ID, or GPA. Any one of those could be the "key". The "key" is the part of the data which on which you sort.
  2. One must be able to compare "keys", otherwise you can't sort. In an implementation, you would have to define an operator< and operator==.
  3. Should elements be repeated? We will assume not.
We will look at several different kinds of data structures: doubly linked lists, dynamic arrays, and binary search trees. Even though each of these data structures are quite different, they will all implement a sorted list. Again, try to keep in mind that, to a user, what's important is the "abstract picture".

Web Accessibility