1 Summary

Implementing the Set ADT with Lists

Even though we haven’t yet done lists in Java, we can sketch this implementation out in pseudocode, relying on your previous understanding of lists. To implement an ADT, we provide a concrete data structure to represent the list and implementations of each operation in the ADT that satisfy the signature and the properties.

Here, for example, is an implementation of Sets basd on Lists:

A set is a list


addElt(Set, element) =

  (cons element Set)


remElt(Set, element) =

  (remove element Set)  \* assume remove built-in on lists *\


size(Set) =

  (length Set)


hasElt(Set, element) =

  (member element Set)

Not quite done though. Before we can claim to implement the Set ADT, we have to show that this implementation respects the properties of Sets. How does this implementation stack up against the two properties?

Moral: when you implement an ADT, make sure that your implementations satisfy the properties. As you discover useful axioms that embody those properties, include them with your ADT definition. They will help you develop good tests later on. We will begin to check that your test cases for ADT implementations check the required properties and axioms.

1 Summary

Key take-away from this example: