Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
3.5. Implementing a Stack in Python¶
Now that we have clearly defined the stack as an abstract data type, we will turn our attention to using Python to implement the stack. Recall that when we give an abstract data type a physical implementation, we refer to the implementation as a data structure.
As we described in Chapter 1, in Python, as in any object-oriented programming language, the implementation of choice for an abstract data type such as a stack is the creation of a new class. The stack operations are implemented as methods. Further, to implement a stack, which is a collection of elements, it makes sense to utilize the power and simplicity of the primitive collections provided by Python. We will use a list.
Recall that the list class in Python provides an ordered collection
mechanism and a set of methods. For example, if we have the list
[2, 5, 3, 6, 7, 4], we need only to decide which end of the list will be
considered the top of the stack and which will be the base. Once that
decision is made, the operations can be implemented using the list
methods such as
The following stack implementation (ActiveCode 1) assumes that
the end of the list will hold the top element of the stack. As the stack
push operations occur), new items will be added on the end
of the list.
pop operations will manipulate that same end.
Remember that nothing happens when we click the
run button other than the
definition of the class. We must create a
Stack object and then use it.
ActiveCode 2 shows the
Stack class in
action as we perform the sequence of operations from
Table 1. Notice that the definition of the
Stack class is
imported from the
pythonds3 module that is included with the materials for this book
or can be downloaded from the Python Package Index.
pythonds3 module contains implementations of all data structures discussed in this book.
It is structured according to the sections: basic, trees, and graphs.
The module can be downloaded from GitHub
or installed from the command line as follows:
python3 -m pip install -U pythonds3
It is important to note that we could have chosen to implement the stack
using a list where the top is at the beginning instead of at the end. In
this case, the previous
append methods would no longer
work and we would have to index position 0 (the first item in the list)
insert. The implementation is shown in
This ability to change the physical implementation of an abstract data
type while maintaining the logical characteristics is an example of
abstraction at work. However, even though the stack will work either
way, if we consider the performance of the two implementations, there is
definitely a difference. Recall that the
operations were both \(O(1)\). This means that the first implementation will
pop in constant time no matter how many items are on
the stack. The performance of the second implementation suffers in that
pop(0) operations will both require \(O(n)\) for a
stack of size
n. Clearly, even though the implementations are logically
equivalent, they would have very different timings when performing
Write a function rev_string(my_str) that uses a stack to reverse the characters in a string.