Python Stacks
Python Stacks
Algorithms Using
Python
Rance D. Necaise
Department of Computer Science
College of William and Mary
194
CHAPTER
Stacks
5
55
23
19
74
23
23
12
74
19
19
23
23
74
74
12
12
(a)
Stacks
illustrates new values being added to the top of the stack and one value being
removed from the top.
Define
Stack ADT
A stack is a data structure that stores a linear collection of items with access
limited to a last-in first-out order. Adding and removing items is restricted to one
end known as the top of the stack. An empty stack is one containing no items.
In the previous chapters, we used the Python list and linked list structures to
implement a variety of container abstract data types. In this chapter, we introduce
the stack, which is a type of container with restricted access that stores a linear
collection. Stacks are very common in computer science and are used in many
types of problems. Stacks also occur in our everyday lives. Consider a stack of
trays in a lunchroom. When a tray is removed from the top, the others shift up.
If trays are placed onto the stack, the others are pushed down.
19
19
CHAPTER 7
12
12
(b)
(c)
19
19
23
23
74
74
12
12
(d)
Figure 7.1: Abstract view of a stack: (a) pushing value 19; (b) pushing value 5; (c)
resulting stack after 19 and 5 are added; and (d) popping top value.
193
pop(): Removes and returns the top item of the stack, if the stack is not empty.
Items cannot be popped from an empty stack. The next item on the stack
becomes the new top item.
push( item ): Adds the given item to the top of the stack.
13
45
19
28
-1
When the outer while loop terminates after the negative value is extracted, the
contents of the stack will be as illustrated in Figure 7.2. Notice the last value
entered is at the top and the first is at the base. If we pop the values from the
stack, they will be removed in the reverse order from which they were pushed onto
the stack, producing a reverse ordering.
28
28
19
19
45
45
13
13
77
195
196
CHAPTER 7
Stacks
Listing 7.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
The individual stack operations are easy to evaluate for the Python list-based
implementation. isEmpty(), len , and peek() only require O(1) time. The
pop() and push() methods both require O(n) time in the worst case since the
underlying array used to implement the Python list may have to be reallocated
to accommodate the addition or removal of the top stack item. When used in
sequence, both operations have an amortized cost of O(1).
7.2.2
197
198
CHAPTER 7
size 4
top
19
19
23
23
74
74
12
12
Stack
Figure 7.3: Sample object of the Stack ADT implemented as a linked list.
The peek() method simply returns a reference to the data item in the first
node after verifying the stack is not empty. If the method were used on the stack
represented by the linked list in Figure 7.3, a reference to 19 would be returned.
The peek operation is only meant to examine the item on top of the stack. It
should not be used to modify the top item as this would violate the definition of
the Stack ADT.
The pop() method always removes the first node in the list. This operation
is illustrated in Figure 7.4(a). This is easy to implement and does not require a
search to find the node containing a specific item. The result of the linked list after
popping the top item from the stack is illustrated in Figure 7.4(b).
The linked list implementation of the Stack ADT is more efficient than the
Python-list based implementation. All of the operations are O(1) in the worst
case, the proof of which is left as an exercise.
size 4
top
19
19
23
23
Stack
The class constructor creates two instance variables for each Stack. The top
field is the head reference for maintaining the linked list while size is an integer
value for keeping track of the number of items on the stack. The latter has to be
adjusted when items are pushed onto or popped off the stack. Figure 7.3 on the
next page illustrates a sample Stack object for the stack from Figure 7.1(b).
The StackNode class is used to create the linked list nodes. Note the inclusion
of the link argument in the constructor, which is used to initialize the next field of
the new node. By including this argument, we can simplify the prepend operation
of the push() method. The two steps required to prepend a node to a linked list
are combined by passing the head reference top as the second argument of the
StackNode() constructor and assigning a reference to the new node back to top.
Stacks
74
74
12
12
(a)
size 33
top
23
23
Stack
74
74
12
12
(b)
Figure 7.4: Popping an item from the stack: (a) the required link modifications, and (b) the
result after popping the top item.
7.3
Stack Applications
The Stack ADT is required by a number of applications encountered in computer
science. In this section, we examine several basic applications that traditionally
are presented in a data structures course.