Stacks
Year 12
Lesson objectives
• Know the difference between a dynamic and a static
and dynamic data structure
• Know how to create, traverse, add and remove data
from a stack
1.4.2 Data Structures, 2.3.1 Algorithms
Static VS Dynamic
Static data structures
• Fixed in size – memory is allocated at compile time; the size cannot
be modified while the program is running
• Suitable for storing a fixed number of elements like monthly sales
• Possible memory waste
• Example: array
Dynamic data structures
• Memory size can be changed (allocated/deallocated) while the
program is running – efficient use of memory
Inefficient use of memory
Array of size 6
Wasted memory
It can't be deallocated during run time
Stacks Stacks are a
last in first out
top
data structure
The addition or
removal of
items always
take place in
one location,
called the top
of the stack
base
The opposite
end is called the
base
Application
• Undo: actions
• Function calls: return
addresses of function
calls to return to the
correct location after
a function executed
Abstract data type (ADT)
• Built-in data types like int, long, float already have
their operations defined (e.g., addition)
• ADT make use of other data structures (such as
arrays) to store data in their own way
• The definition of ADT mentions what operations
can be performed but not how the operations
will be implemented – up to the programmer
oAbstraction: providing only the essentials and
hiding the details
Depending on the implementation, stacks can be a
static or dynamic data structure
Operations
Operation Purpose
push(data) Adds data onto the top of the
stack.
pop() Removes and returns the top
element.
peek() Returns the top element
without removing it from the
stack.
size() Returns the number of
elements in the stack.
isEmpty() Returns true if the size is 0.
isFull() Returns true if the stack is full.
Stacks implemented with arrays
• Two attributes:
o top
o maxSize
• As you push/pop
elements, change
the top pointer
accordingly
Stacks implemented with arrays
Initializing
1. top = -1
2. maxSize =3
Two purposes for having a 'top' pointer 2
3. Check if we have reached the limit
1
4. Keep track of where to pop/push
0
Why is top –1 instead of 0?
Stacks implemented with arrays
push(10)
Variables
top = 0 2
maxSize = 3
1
0 10
Stacks implemented with arrays
push(20)
Variables
top = 1 2
maxSize = 3
1 20
0 10
Stacks implemented with arrays
push(30)
If your stack using an array is
print peek() 30 implemented properly,
attempting to push another
element should return some
Variables error.
top = 2 2 30
maxSize = 3
1 20
0 10
Stacks implemented with arrays
pop()
print peek() 20
Variables
top = 1 2
maxSize = 3
1 20
0 10
Pseudocode: stack using array
Stacks implemented with lists
(dynamic)
stack = []
stack.append('apple') push(data)
stack.pop() pop()
len(stack) size()
These above are the built-in functions for lists in Python – you have
to implement the other operations
• peek()
• isEmpty()
isFull() does not apply for a dynamic data structure
Worksheet
• Complete the worksheet - due next Thursday
morning.
Email your Python solution to n.shi@lsfed.com by
Thursday 9:15AM
• If you finish early, there are challenge questions on
the next slides
Challenge
Solve using a stack – write
the pseudocode
Stuck? Hints on the next
few slides
Extra challenge: write it out
in Python
Examples
An input string is valid if: • Input s = "[]() • Input s = "[{}"
1. Open brackets must be {}"
Output: false
closed by the same type Output: true
(rule 1)
of brackets.
• Input s = "([])" • Input s: "{(})"
2.Open brackets must be Output: true Output: false
closed in the correct order. (rule 2)
3.Every close bracket has a • Input s: "[]]"
corresponding open
Output: false
bracket of the same type. (rule 3)
Hint 1
• When you see an opening bracket, push it onto the
stack.
Still stuck? Hint 2 on the next slide
Hint 2
• When you see a right bracket, pop from the stack
and make some decision.
Last hint on the next slide
Hint 3
• If you have __ elements left in the stack after
traversing the string, it means that there are more
left brackets than right. That is not a valid input
string.
How would you solve
using a stack? Write the
Challenge #2 pseudocode