Data Structures - Lab 06
Tuesday, September 29th
Implementing and using a stack
Yesterday we started considering stacks, and we wrote two different implementations of a stack that were built on top of Python's underlying list data structure.
On top of this, your author has distributed code for two further implementations built on top of the Array class (ArrayStack) that we wrote in chapter 13 and a linked list (LinkedStack).
All four of these implementations are contained in a file called stacks.py which is contained in the following zip file:
Getting Started
Stack (bottomZeroStack)
Let's begin by considering the code for the class called Stack - where the bottom of the stack is at zero.
This means that the data looks something like this if we begin with an empty Stack
0 1 2 3 4 ...
And then push(10)
0 1 2 3 4 ... 10
And then push(4)
0 1 2 3 4 ... 10 4
And then push(22)
0 1 2 3 4 ... 10 4 22
And then push(36)
0 1 2 3 4 ... 10 4 22 36
And finally pop()
0 1 2 3 4 ... 10 4 22
[Q1] Using what you know about Stacks and using the code/implementation provided in stacks.py, predict the Big-Oh notation for this Stack implementation. In other words, will push take longer if there are 10 items on the list versus if there are 1000 items on the list? What about the other methods?
| push(item) | pop() | peek() | size() | isEmpty() | |
| Big-Oh |
AltStack (topZeroStack)
Next, let's consider the code called AltStack - where the top of the Stack is at zero (topZeroStack.py).
We begin with an empty Stack
0 1 2 3 4 ...
And then push(10)
0 1 2 3 4 ... 10
And then push(4)
0 1 2 3 4 ... 4 10
And then push(22)
0 1 2 3 4 ... 22 4 10
And then push(36)
0 1 2 3 4 ... 36 22 4 10
And finally pop()
0 1 2 3 4 ... 22 4 10
[Q2] Using what you know about Stacks and using the code/implementation provided in stacks.py, predict the Big-Oh notation for this Stack implementation. In other words, will push take longer if there are 10 items on the list versus if there are 1000 items on the list? What about the other methods?
| push(item) | pop() | peek() | size() | isEmpty() | |
| Big-Oh |
ArrayStack
Next, let's consider the code called ArrayStack which used the Array class that we worked with in chapter 13. Take some time to look at this code and see how it works
[Q3] How is this code similar to Stack?
[Q4] How is this code different from Stack?
[Q5] Which one should be faster for most operations?
[Q6] Using what you know about Stacks and using the code/implementation provided in stacks.py, predict the Big-Oh notation for this Stack implementation. In other words, will push take longer if there are 10 items on the list versus if there are 1000 items on the list? What about the other methods?
| push(item) | pop() | peek() | size() | isEmpty() | |
| Big-Oh |
LinkedStack
Next, let's consider the code called LinkedStack which used the Node class from chapter 13 to create a stack using a linked list. Take some time to look at this code and see how it works
[Q7] In terms of the underlying linked list, where is the top of the stack with this implementation?
[Q8] Take a few minutes to look at the code for __str__. What is very different about this code from anything we have seen before? What is going on in this method?
[Q9] Using what you know about Stacks and using the code/implementation provided in stacks.py, predict the Big-Oh notation for this Stack implementation. In other words, will push take longer if there are 10 items on the list versus if there are 1000 items on the list? What about the other methods?
| push(item) | pop() | peek() | size() | isEmpty() | |
| Big-Oh |
Benchmarking the runtime of each of these implementations
Let's see if you were correct in your answers to Q1, Q2, Q6 and Q9.
Open the file named timeStackTest.py. Currently, this piece of code should:
[Q10] Run this piece of code. How long did it take to push 5,000 items onto a Stack already containing 10,000 items using the bottomZeroStack?
[Q11] Now modify your code to push 5,000 items onto a Stack already containing 20,000 items using the bottomZeroStack. How long did that take?
[Q12] Run several variations of this test to complete the table below:
| Stack (bottomZeroStack) | AltStack (topZeroStack) | ArrayStack | LinkedStack | |||||
|
Starts with |
10,000 | 20,000 | 10,000 | 20,000 | 10,000 | 20,000 | 10,000 | 20,000 |
| push() *5000 | ||||||||
| pop() *5000 | ||||||||
| peek() *5000 | ||||||||
| size() *5000 | ||||||||
| isEmpty() *5000 | ||||||||
[Q13] Summarize the major differences you observed.
So far, everything we have done has involved specific implementations of a stack, but we haven't actually worked with USING a stack
The code called "brackets.py" contained on page 567-8 in your textbook was provided in the code you downloaded at the start of the lab. It is a chunk of code that uses a stack to see if an expression contains balanced brackets. Open this file in IDLE and look at the code.
[Q14] What does this code do when it encounters "opening" brackets?
[Q15] What does this code do when it encounters "closing" brackets?
[Q16] What does this code do when it encounters non-brackets?
[Q17] Under what circumstances would the conditional be True in the line of code "if stk.isEmpty():"
[Q18] Consider the final return statement ("return stk.isEmpty()") Under what circumstance would the stack be empty at that point (what would the expression have looked like?)
[Q19] Consider the final return statement ("return stk.isEmpty()") Under what circumstance would the stack be non-empty at that point (what would the expression have looked like?)
[Q20] For each of the expression that you listed below, run this through the checker and record whether it is balanced or not (pay attention to what the correct answer should be and make sure you get what you expected). For each expression CIRCLE the last character in the expression that was actually considered by the algorithm
| Original expression | Result | Last character considered |
| ( 3+2 ) * [8-2] | ( 3 +2 ) * [8-2] | |
| ( 3 + 2) * ( 8 - | ( 3 + 2) * ( 8 - | |
| ) 3 + 2 ( * [ 8 -2] | ) 3 + 2 ( * [ 8 -2] | |
| [ 3 * (4 + 2) - 3 ] | [ 3 * (4 + 2) - 3 ] | |
| [ 3 * (4 + 2] - 3 ) | [ 3 * (4 + 2] - 3 ) |
To get full credit for this lab you must complete all questions and obtain all signatures by the START of class on Wednesday.