8 Stack
8 Stack
Sungmin Cha
New York University
09.30.2024
Outline
• Review the previous lecture
2
Outline
• Review the previous lecture
3
From List to Stack/Queue
• Overview of Data Structures
– Linear data structures
List
: Type of structures (Array/Linked) and operations
Stack – Last In First Out (LIFO)
Queue – First In First Out (FIFO) Stack and Queue are
implemented using a list!
– Non-linear data structures
Tree
Graph
4
Stack
• The concept and principles of a stack
– Access to the stack is limited to the element at the top
The stack always maintains the location of the top
8 top
42
12
Stack 5
Stack
• The concept and principles of a stack
– Push(): add an element to the top of the collection
22
Push()
8 top
42
12
Stack 6
Stack
• The concept and principles of a stack
– Push(): add an element to the top of the collection
After the pop, the top should be changed
22
Push()
22 top
8 top 8
42 42
1 1
12 12
Stack Stack 7
Stack
• The concept and principles of a stack
– Pop(): remove the recently added element
Pop()
22 top
42
12
Stack 8
Stack
• The concept and principles of a stack
– Pop(): remove the recently added element
After the removal, the top should be changed
22 Pop()
22 top
8 8 top
42 42
1 1
12 12
Stack Stack 9
Stack
• The concept and principles of a stack
– top(): get an element from the stack
8 top
42
12
Stack 10
Stack
• The concept and principles of a stack
– top(): get an element from the stack
If we request an element from a stack, the stack always provides the one
at the top
8
top()
8 top
42
12
Stack 11
Stack
• The concept and principles of a stack
– Last In First Out (LIFO)
The element most recently ‘pushed’ is the first one to be ‘popped’
Empty Stack
12
Stack
• The concept and principles of a stack
– Last In First Out (LIFO)
The element most recently ‘pushed’ is the first one to be ‘popped’
1 top
12 top 12
1 top
12 top 12 12 top
Init Stack
push
pop
print elements
15
Outline
• Review the previous lecture
16
Programming Problems
• Reversing a String
– Let’s implement a function which reverses the order of a given
string using a stack
– Example:
Input – “12345” => output – “54321”
Input – “a1b2c3” => output – “3c2b1a”
Input – “Test Seq 12345” => output – “54321 qeS tseT”
– Example code
Ed Workspace/Lec 8 - Reversing a String
17
Programming Problems
• Reversing a String
– Example code:
Expected output:
18
Programming Problems
• Hint? Last in First out
– Example: Input – “12345” => output – “54321”
19
Programming Problems
• Balance Checker
– Let’s implement a function that solves matching bracket
problems for three types of brackets
Brackets: () / {} / []
– Example:
Input – “{ { ( [ ] [ ] ) } ( ) }” => output – “true”
Input – “[ [ { { ( ( ) ) } } ] ]” => output – “true”
Input – “( ( ( ) ] ) )” => output – “false”
– Example code
Ed Workspace/Lec 8 - Balance Checker
20
Programming Problems
• Hint?
– Matches function
21
Programming Problems
• Daily Temperature
– Let’s implement a function that takes a daily temperature list
and then outputs the number of days one would need to wait
for warmer weather
– Example:
Input – “[23, 24, 25, 21, 19, 22, 26, 23]”
=> output – “[1, 1, 4, 2, 1, 1, 0, 0]”
– Example code
Ed Workspace/Lec 8 - Daily Temperature
22
Programming Problems
• Hint?
– Input – “temp = [23, 24, 25, 21, 19, 22, 26, 23]”
Consider we access each element sequentially
1) All dates must be recorded, 2) The index represents the date
`C
26
4
24
1
1
22
2 1
20
1
18
0 Index (i) 23
Thank you!
E-mail: [email protected]
24