0% found this document useful (0 votes)
6 views24 pages

8 Stack

This lecture focuses on the concept of stacks, including their implementation and applications in solving programming problems such as reversing strings, checking for balanced brackets, and calculating daily temperatures. It explains the Last In First Out (LIFO) principle of stacks and provides example code for practical understanding. The lecture also includes a review of linear and non-linear data structures, emphasizing the relationship between lists, stacks, and queues.

Uploaded by

thirtythr33spam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views24 pages

8 Stack

This lecture focuses on the concept of stacks, including their implementation and applications in solving programming problems such as reversing strings, checking for balanced brackets, and calculating daily temperatures. It explains the Last In First Out (LIFO) principle of stacks and provides example code for practical understanding. The lecture also includes a review of linear and non-linear data structures, emphasizing the relationship between lists, stacks, and queues.

Uploaded by

thirtythr33spam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Data Structures

<Lecture 8: Stack 2>

Sungmin Cha
New York University

09.30.2024
Outline
• Review the previous lecture

• Solving programming problems using a stack


– Stack
– Implementation of Stack
– Application [Solving Programming Problem]

2
Outline
• Review the previous lecture

• Solving programming problems using a stack


– Stack
– Implementation of Stack
– Application [Solving Programming Problem]

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

Empty Stack push(12) push(1)


13
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 12 top

Empty Stack push(12) push(1) pop()


14
Stack Application
• Using stack class in JAVA
Import a package to use Stack
– Example code:

Init Stack

push
pop

print elements

15
Outline
• Review the previous lecture

• Solving programming problems using a stack


– Reversing a string
– Balance checker
– Daily temperature

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

– It is necessary to check whether each left-directional


bracket “(, [, {“ corresponds correctly to its opposite right-
directional bracket “), ], }”

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

You might also like