Warning: the HTML version of this document is generated from
Latex and may contain translation errors. In
particular, some mathematical expressions are not translated correctly.
18.1 Abstract data types
The data types you have seen so far are all concrete, in the sense that we have completely specified how they are implemented. For example, the Card class represents a card using two integers. As we discussed at the time, that is not the only way to represent a card; there are many alternative implementations.
An abstract data type, or ADT, specifies a set of operations (or methods) and the semantics of the operations (what they do), but it does not specify the implementation of the operations. That's what makes it abstract.
Why is that useful?
18.2 The Stack ADT
In this chapter, we will look at one common ADT, the stack. A stack is a collection, meaning that it is a data structure that contains multiple elements. Other collections we have seen include dictionaries and lists.
An ADT is defined by the operations that can be performed on it, which is called an interface. The interface for a stack consists of these operations:
18.3 Implementing stacks with Python lists
The list operations that Python provides are similar to the operations that define a stack. The interface isn't exactly what it is supposed to be, but we can write code to translate from the Stack ADT to the built-in operations.
This code is called an implementation of the Stack ADT. In general, an implementation is a set of methods that satisfy the syntactic and semantic requirements of an interface.
Here is an implementation of the Stack ADT that uses a Python list:
class Stack :
A Stack object contains an attribute named items that is a list of items in the stack. The initialization method sets items to the empty list.
To push a new item onto the stack, push appends it onto items. To pop an item off the stack, pop uses the homonymous * Note list method to remove and return the last item on the list.
Finally, to check if the stack is empty, isEmpty compares items to the empty list.
An implementation like this, in which the methods consist of simple invocations of existing methods, is called a veneer. In real life, veneer is a thin coating of good quality wood used in furniture-making to hide lower quality wood underneath. Computer scientists use this metaphor to describe a small piece of code that hides the details of an implementation and provides a simpler, or more standard, interface.
18.4 Pushing and popping
A stack is a generic data structure, which means that we can add any type of item to it. The following example pushes two integers and a string onto the stack:
>>> s = Stack()
We can use isEmpty and pop to remove and print all of the items on the stack:
while not s.isEmpty() :
The output is + 45 54. In other words, we just used a stack to print the items backward! Granted, it's not the standard format for printing a list, but by using a stack, it was remarkably easy to do.
You should compare this bit of code to the implementation of printBackward in Section 17.4. There is a natural parallel between the recursive version of printBackward and the stack algorithm here. The difference is that printBackward uses the runtime stack to keep track of the nodes while it traverses the list, and then prints them on the way back from the recursion. The stack algorithm does the same thing, except that it uses a Stack object instead of the runtime stack.
18.5 Using a stack to evaluate postfix
In most programming languages, mathematical expressions are written with the operator between the two operands, as in 1+2. This format is called infix. An alternative used by some calculators is called postfix. In postfix, the operator follows the operands, as in 1 2 +.
The reason postfix is sometimes useful is that there is a natural way to evaluate a postfix expression using a stack:
As an exercise, apply this algorithm to the expression 1 2 + 3 *.
This example demonstrates one of the advantages of postfix
As an exercise, write a postfix expression that is equivalent to 1 + 2 * 3.
To implement the previous algorithm, we need
to be able to traverse a string and break it into operands and
operators. This process is an example of parsing, and the
Python provides a split method in both the string and re (regular expression) modules. The function string.split splits a string into a list using a single character as a delimiter. For example:
>>> import string
In this case, the delimiter is the space character, so the string is split at each space.
The function re.split is more powerful, allowing us to provide a regular expression instead of a delimiter. A regular expression is a way of specifying a set of strings. For example, [A-z] is the set of all letters and [0-9] is the set of all digits. The ^ operator negates a set, so [^0-9] is the set of every character that is not a digit, which is exactly the set we want to use to split up postfix expressions:
>>> import re
Notice that the order of the arguments is different from string.split; the delimiter comes before the string.
The resulting list includes the operands 123 and 456 and the operators * and /. It also includes two empty strings that are inserted as "phantom operands," whenever an operator appears without a number before or after it.
18.7 Evaluating postfix
To evaluate a postfix expression, we will use the parser from the previous section and the algorithm from the section before that. To keep things simple, we'll start with an evaluator that only implements the operators + and *:
The first condition takes care of spaces and empty strings. The next two conditions handle operators. We assume, for now, that anything else must be an operand. Of course, it would be better to check for erroneous input and report an error message, but we'll get to that later.
Let's test it by evaluating the postfix form of (56+47)*2:
>>> print evalPostfix ("56 47 + 2 *")
18.8 Clients and providers
One of the fundamental goals of an ADT is to separate the
interests of the provider, who writes the code that implements
the ADT, and the client, who uses the ADT.
The provider only has to worry
about whether the implementation is correct
Conversely, the client assumes that the implementation of the ADT is correct and doesn't worry about the details. When you are using one of Python's built-in types, you have the luxury of thinking exclusively as a client.
Of course, when you implement an ADT, you also have to write client code to test it. In that case, you play both roles, which can be confusing. You should make some effort to keep track of which role you are playing at any moment.
Warning: the HTML version of this document is generated from Latex and may contain translation errors. In particular, some mathematical expressions are not translated correctly.