0% found this document useful (0 votes)
22 views50 pages

10 Queue

The lecture covers the concepts of queues and stacks, emphasizing their principles, time complexities, and programming applications. It includes a detailed explanation of the FIFO principle for queues and LIFO for stacks, along with their implementations using arrays and linked lists. Additionally, it discusses a programming problem involving daily temperatures and how to solve it using stacks.

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)
22 views50 pages

10 Queue

The lecture covers the concepts of queues and stacks, emphasizing their principles, time complexities, and programming applications. It includes a detailed explanation of the FIFO principle for queues and LIFO for stacks, along with their implementations using arrays and linked lists. Additionally, it discusses a programming problem involving daily temperatures and how to solve it using stacks.

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/ 50

Data Structures

<Lecture 10: Queue 2>

Sungmin Cha
New York University

02.27.2024
Outline
• Notice

• Review the Previous Lecture

• Queue and Stack


– Summary
– Time complexity
– Programming problems

2
Outline
• Notice

• Review the Previous Lecture

• Queue and Stack


– Summary
– Time complexity
– Programming problems

3
Notice
• Future schedule

– Lecture
 Today's lecture: Stack and Queue (+ problems)
 Lecture 11-14: Sorting Algorithm + Heap
– HW2
– Midterm Exam (10/28 Mon)

4
Notice
• Midterm exam
– 12:30 PM on Monday, October 28th
– Location: 60 Fifth Ave, 110
– The Midterm exam covers Lecture 1-14
– Offline exam and closed book
– Include True/False and multiple-choice questions, subjective
questions, and filling in appropriate pseudo code for blanks
 There will be 1-2 challenging problems
 All test questions will be created based on lecture slides and HW1-2
(Labs are not considered)
– I will provide the example of midterm exam on October 21

5
Outline
• Notice

• Review the Previous Lecture

• Queue and Stack


– Summary
– Time complexity
– Programming problems

6
Queue
• The concept and principles of a queue
– First-In-First-Out (FIFO)
 The element that is first enqueued is dequeued first

Empty
queue

7
Queue
• The concept and principles of a queue
– First-In-First-Out (FIFO)
 The element that is first enqueued is dequeued first

Empty
queue
front tail
Enqueue(20),
Enqueue(5),
Enqueue(15) 20 5 15

8
Queue
• The concept and principles of a queue
– First-In-First-Out (FIFO)
 The element that is first enqueued is dequeued first

Empty
queue
front tail
Enqueue(20),
Enqueue(5),
Enqueue(15) 20 5 15

front tail

Dequeue()
5 15

9
Array Queue
• Queue implementation using an array
– Inefficiency in space utilization
 enqueue(9)

front tail

7 8 9
Queue is full?
Empty space

 How can we overcome this problem?

10
Array Queue
• Queue implementation using an array
– Viewing an array as a circle
 Initial state
front
tail
n-1 0

6 1

5 2

4 3 11
Queue
• Implementation of Queue
– Using Array
: viewing as a circle
– Using LinkedList

• Time complexity of operations of a queue


– O(1)

12
Queue Application
• Using Queue class in JAVA
Import a package to use Queue
– Example code:

Init Queue

enqueue
dequeue

print elements

13
Outline
• Notice

• Review the Previous Lecture

• Queue and Stack


– Summary
– Time complexity
– Programming problems

14
Summary
• List
– A common data structure that stores elements in sequential
order
– It allows for the addition, deletion, and modification of
elements freely
– Access to specific elements is possible using an index

15
Summary
• Stack
– A data structure that adheres to the Last-In-Fist-Out (LIFO)
principle
– Elements are added to the top of the stack, and removal
occurs only from the top
– Operations such as Push (adding) and Pop (removing) are used
to manipulate elements
– A stack can be implemented by Array or LinkedList

16
Summary
• Queue
– A data structure that follows the First-In-First-Out (FIFO)
principle
– Elements are added to the end of the queue, and removed
from the front
– Operations such as Enqueue (adding) and Dequeue (removing)
are used to manipulate elements
– A queue can be implemented by Array or LinkedList

17
Time Complexity (Big-O)
• Comparing time complexity (Big-O)
getNode() Add() Append() Remove() Get()
ArrayList - O(n) O(1) O(n) O(1)
[O(n) when array
is full]
LinkedList O(n) O(n) O(n) O(n) O(n)
Stack - O(1) - O(1) O(1)
(ArrayList) [push(x), O(n) [pop()] [top()]
when array is full]

Stack - O(1) - O(1) O(1)


(LinkedList) [push(x)] [pop()] [top()]
Queue - O(1) - O(1) O(1)
(ArrayList) [enqueue(x), O(n) [dequeue()] [front()]
when array is full]

Queue - O(1) - O(1) O(1)


(Singly-linked [enqueue(x)] [dequeue()] [front()]
circular list)

18
Programming Problems (review)
• 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

19
Programming Problems (review)
• 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) 20
Programming Problems (review)
• Solution
– Input – “temp = [23, 24, 25, 21, 19, 22, 26, 23]”
– Consider we access each element sequentially
`C
1. Access to each index sequentially
2. Compare temp between the current 26
4
index and the last stored index (pop)
1. If temp increases: save the difference
24
2. If not: push the current index to the stack 1
1
22
2 1

Core idea: Use a stack to store 20


indices for which the dates have
not yet been calculated 1
18
0 Index (i)
21
Programming Problems (review)
• Solution
– Input – “temp = [23, 24, 25, 21, 19, 22, 26, 23]”
– Consider we access each element sequentially
 i=0 `C
o Stack.push(i=0) 26

24

22
i=0

20

18
0 Index (i)
0
result = [0, 0, 0, 0, 0, 0, 0, 0] 22
Programming Problems (review)
• Solution
– Input – “temp = [23, 24, 25, 21, 19, 22, 26, 23]”
– Consider we access each element sequentially
 i=1 `C
o Temp[i] > temp [stack.top()] 26
0
24
The index of the date for which
the number of days until
the temp. rise has not been determined 22
i=1

20

18
0 Index (i)
0
result = [0, 0, 0, 0, 0, 0, 0, 0] 23
Programming Problems (review)
• Solution
– Input – “temp = [23, 24, 25, 21, 19, 22, 26, 23]”
– Consider we access each element sequentially
 i=1 `C
o Temp[i] > temp [stack.top()] 26
o = 24 > 23
24

22
i=1

20

18
0 Index (i)
0
result = [0, 0, 0, 0, 0, 0, 0, 0] 24
Programming Problems (review)
• Solution
– Input – “temp = [23, 24, 25, 21, 19, 22, 26, 23]”
– Consider we access each element sequentially
 i=1 `C
o Temp[i] > temp [stack.top()] 26
» int last = stack.pop()
24

the number of days is determined


22
i=1

20

18
0 Index (i)
result = [0, 0, 0, 0, 0, 0, 0, 0] 25
Programming Problems (review)
• Solution
– Input – “temp = [23, 24, 25, 21, 19, 22, 26, 23]”
– Consider we access each element sequentially
 i=1 `C
o Temp[i] > temp [stack.top()] 26
» int last = stack.pop()
» result[last] = i - last 24
1 0

22
Store the number of days at that date
i=1

20

18
0 Index (i)
result = [1, 0, 0, 0, 0, 0, 0, 0] 26
Programming Problems (review)
• Solution
– Input – “temp = [23, 24, 25, 21, 19, 22, 26, 23]”
– Consider we access each element sequentially
 i=1 `C
o Stack.push(i) 26
» Repeat the previous steps
24

22
i=1

20

18
0 Index (i)
1
result = [1, 0, 0, 0, 0, 0, 0, 0] 27
Programming Problems (review)
• Solution
– Input – “temp = [23, 24, 25, 21, 19, 22, 26, 23]”
– Consider we access each element sequentially
 i=3 `C
o Temp[i] > temp [stack.top()] 26
» 21 < 25: Not true
24

22

20

i=3
18
0 Index (i)
2
result = [1, 1, 0, 0, 0, 0, 0, 0] 28
Programming Problems (review)
• Solution
– Input – “temp = [23, 24, 25, 21, 19, 22, 26, 23]”
– Consider we access each element sequentially
 i=3 `C
o Temp[i] > temp [stack.top()] 26
» 21 < 25: Not true
o Stack.push(i) 24

22

20

i=3
3 18
0 Index (i)
2
result = [1, 1, 0, 0, 0, 0, 0, 0] 29
Programming Problems (review)
• Solution
– Input – “temp = [23, 24, 25, 21, 19, 22, 26, 23]”
– Consider we access each element sequentially
 i=4 `C
o Temp[i] > temp [stack.top()] 26
» 19 < 21: Not true
24

22

20

3 18
0 Index (i)
2 i=4
result = [1, 1, 0, 0, 0, 0, 0, 0] 30
Programming Problems (review)
• Solution
– Input – “temp = [23, 24, 25, 21, 19, 22, 26, 23]”
– Consider we access each element sequentially
 i=4 `C
o Temp[i] > temp [stack.top()] 26
» 19 < 21: Not true
o Stack.push(i) 24

22

20
4

3 18
0 Index (i)
2 i=4
result = [1, 1, 0, 0, 0, 0, 0, 0] 31
Programming Problems (review)
• Solution
– Input – “temp = [23, 24, 25, 21, 19, 22, 26, 23]”
– Consider we access each element sequentially
 i=5 `C
o Temp[i] > temp [stack.top()] 26
» 22 > 19: true
24

22

20 i=5
4

3 18
0 Index (i)
2
result = [1, 1, 0, 0, 0, 0, 0, 0] 32
Programming Problems (review)
• Solution
– Input – “temp = [23, 24, 25, 21, 19, 22, 26, 23]”
– Consider we access each element sequentially
 i=5 `C
o Temp[i] > temp [stack.top()] 26
» int last = stack.pop()
4 24

22

20 i=5

3 18
0 Index (i)
2
result = [1, 1, 0, 0, 0, 0, 0, 0] 33
Programming Problems (review)
• Solution
– Input – “temp = [23, 24, 25, 21, 19, 22, 26, 23]”
– Consider we access each element sequentially
 i=5 `C
o Temp[i] > temp [stack.top()] 26
» int last = stack.pop()
» result[last] = i - last 24
5 4

22

20 i=5

3 18
0 Index (i)
2
result = [1, 1, 0, 0, 1, 0, 0, 0] 34
Programming Problems (review)
• Solution
– Input – “temp = [23, 24, 25, 21, 19, 22, 26, 23]”
– Consider we access each element sequentially
 i=5 `C
o Temp[i] > temp [stack.top()] 26
» 22 > 21: true
24

22

20 i=5

3 18
0 Index (i)
2
result = [1, 1, 0, 0, 1, 0, 0, 0] 35
Programming Problems (review)
• Solution
– Input – “temp = [23, 24, 25, 21, 19, 22, 26, 23]”
– Consider we access each element sequentially
 i=5 `C
o Temp[i] > temp [stack.top()] 26
» int last = stack.pop()
3 24

22

20 i=5

18
0 Index (i)
2
result = [1, 1, 0, 2, 1, 0, 0, 0] 36
Programming Problems (review)
• Solution
– Input – “temp = [23, 24, 25, 21, 19, 22, 26, 23]”
– Consider we access each element sequentially
 i=5 `C
o Temp[i] > temp [stack.top()] 26
» int last = stack.pop()
» result[last] = i - last 24
5 3

22

20 i=5

18
0 Index (i)
2
result = [1, 1, 0, 2, 1, 0, 0, 0] 37
Programming Problems (review)
• Solution
– Input – “temp = [23, 24, 25, 21, 19, 22, 26, 23]”
– Consider we access each element sequentially
 i=5 `C
o Temp[i] > temp [stack.top()] 26
» 22 > 25: false
24

22

20 i=5

18
0 Index (i)
2
result = [1, 1, 0, 2, 1, 0, 0, 0] 38
Programming Problems (review)
• Solution
– Input – “temp = [23, 24, 25, 21, 19, 22, 26, 23]”
– Consider we access each element sequentially
 i=5 `C
o Temp[i] > temp [stack.top()] 26
o Stack.push(i)
24

22

20 i=5

5 18
0 Index (i)
2
result = [1, 1, 0, 2, 1, 0, 0, 0] 39
Programming Problems (review)
• Solution
– Input – “temp = [23, 24, 25, 21, 19, 22, 26, 23]”
– Consider we access each element sequentially
 Repeat i = 0 to temp.length `C
26

24

22

20 i=5

5 18
0 Index (i)
2
result = [1, 1, 0, 2, 1, 0, 0, 0] 40
Programming Problems (review)
• Solution

41
Programming Problems
• Checking for palindromic strings
– A palindromic string refers to a string that reads the same
forwards as it does backward
– Implement a function that utilizes a stack and a queue to check
whether a given string is a palindromic string

– Example:
 Input: ‘abbcbba’ -> output: ‘true’
 Input: ‘abb’ -> output: ‘false’

– Example code
‘Ed workspace/Palindromic Strings’

42
Programming Problems
• Solution: checking for palindromic strings
– Example: ‘abbcbba’
 What to compare?

a b b c b b a

’a’ == ‘a’

43
Programming Problems
• Solution: checking for palindromic strings
– Example: ‘abbcbba’
 What to compare?

a b b c b b a

’b’ == ‘b’

44
Programming Problems
• Solution: checking for palindromic strings
– Example: ‘abbcbba’
 What to compare?

a b b c b b a

’b’ == ‘b’

45
Programming Problems
• Solution: checking for palindromic strings
– Example: ‘abbcbba’
 What to compare?

a b b c b b a

’c’ == ‘c’

– Comparison rule
: compare i-th with (N-i)-th element

46
Programming Problems
• Solution: checking for palindromic strings
– Example: ‘abbcbba’
 False case

a b b c b d a

’b’ != ‘d’

– How can we compare them using Stack and Queue?


 Stack: LIFO
 Queue: FIFO

47
Programming Problems
• N cards
– There are N cards numbered from 1 to N, arranged in order
with the first card on top and the N-th card at the bottom
– The following actions are repeated until only one card remains:
Firstly, discard the top card, and then move the new top card to
the bottom
– Write a program to output the discarded cards in order and the
last remaining card based on the given value of N

48
Programming Problems
• N cards
– Example
 Let's consider a scenario with N=4
 The cards are initially arranged in order from the top as 1234
 Discarding the first card results in 234 remaining
 Moving the second card to the bottom transforms the sequence into 342
 After discarding the third card, we are left with 42, and placing the fourth
card beneath it gives us 24
 Finally, discarding the second card leaves us with the discarded cards in
the order 1 3 2, and the remaining card is 4.
 In conclusion, the output should be ‘1 3 2 4’

– Example code
 Ed workspace/NCards
49
Thank you!

E-mail: [email protected]

50

You might also like