Download presentation
Presentation is loading. Please wait.
1
Circular Queues: Implemented using Arrays
Damian Gordon
2
Circular Queues We can also have a circular queue:
3
Circular Queues We can also have a circular queue:
A queue where the start and end of the queue are joined together.
4
Circular Queues Head Tail We can also have a circular queue: 7 43 6 1
43 6 1 12 35 5 2 22 99 4 Tail 3
5
Circular Queues Head Tail We can also have a circular queue: 7 6 1 12
6 1 12 35 5 2 22 99 4 Tail 3
6
Circular Queues Head Tail We can also have a circular queue: 7 6 1 12
6 1 12 68 35 5 2 Tail 22 99 4 3
7
Circular Queues Head Tail We can also have a circular queue: 7 6 1 12
6 1 Tail 12 54 68 35 5 2 22 99 4 3
8
Circular Queues Tail Head We can also have a circular queue: 7 6 1 54
6 1 Tail 54 Head 68 35 5 2 22 99 4 3
9
Circular Queues Tail Head We can also have a circular queue: 7 17 6 1
17 6 1 54 Head 68 35 5 2 22 99 4 3
10
Circular Queues Tail Head We can also have a circular queue: 7 17 81 6
17 81 6 1 54 Head 68 35 5 2 22 99 4 3
11
Circular Queues Tail Head We can also have a circular queue: 7 17 81 6
17 81 6 1 54 43 Head 68 35 5 2 22 99 4 3
12
Simple Simulation
13
Queues Queue Head Tail MaxSize
We will implement a queue as an array called Queue. Head Tail 1 2 3 4 5 6 Queue MaxSize
14
Queues Queue Head Tail MaxSize
We will implement a queue as an array called Queue. Head Tail 1 2 3 4 5 6 Queue MaxSize
15
Queues Queue Head Tail MaxSize
We will implement a queue as an array called Queue. Head Tail 1 2 3 4 5 6 Queue MaxSize
16
Queues Queue Head Tail MaxSize
We will implement a queue as an array called Queue. Head Tail 1 2 3 4 5 6 Queue MaxSize
17
Queues Queue Head Tail MaxSize
We will implement a queue as an array called Queue. Head Tail 1 2 3 4 5 6 Queue MaxSize
18
Queues Queue Head Tail MaxSize
We will implement a queue as an array called Queue. Head Tail 1 2 3 4 5 6 Queue MaxSize
19
Queues Queue Head Tail MaxSize
We will implement a queue as an array called Queue. Tail Head 1 2 3 4 5 6 Queue MaxSize
20
End of Simulation
21
Circular Queues Queue Head Tail MaxSize
How does that work, let’s DeleteFromQ() Head Tail 31 1 41 2 59 3 26 53 4 5 6 Queue MaxSize
22
Circular Queues Queue Head Tail MaxSize Now AddtoQ(55) 41 1 2 59 26 3
41 1 2 59 26 3 53 4 5 6 Queue MaxSize
23
Circular Queues Queue Head Tail MaxSize Now let’s DeleteFromQ() 1 41 2
1 41 2 59 3 26 4 53 5 55 6 Queue MaxSize
24
Circular Queues Queue Head Tail MaxSize Now AddtoQ(34) 1 2 59 26 3 53
1 2 59 26 3 53 4 5 55 6 Queue MaxSize
25
Circular Queues Queue Head Tail MaxSize Now AddtoQ(12) 1 2 59 3 26 4
1 2 59 3 26 4 53 5 55 6 34 Queue MaxSize
26
Circular Queues Tail Head 12 1 2 59 3 26 4 53 5 55 6 34 Queue MaxSize
27
Circular Queues So Tail starts at 4, goes to 5, goes to 6, goes to 0, goes to 1, etc. So it’s Tail = Tail + 1, But…
28
Circular Queues So Tail starts at 4, goes to 5, goes to 6, goes to 0, goes to 1, etc. So it’s Tail = Tail + 1, But… To be circular it goes as follows: 4->5->6->0->1->2->3->4->5->6->0->1->2->3->4->5->6->0->1->etc.
29
Circular Queues So Tail starts at 4, goes to 5, goes to 6, goes to 0, goes to 1, etc. So it’s Tail = Tail + 1, But… Tail = (Tail + 1) % 7
30
Circular Queues So Tail starts at 4, goes to 5, goes to 6, goes to 0, goes to 1, etc. So it’s Tail = Tail + 1, But… Tail = (Tail + 1) % 7 So Tail % 7 works as follows: 4 % 7 = 4 5 % 7 = 5 6 % 7 = 6 7 % 7 = 0 8 % 7 = 1
31
Circular Queues So Tail starts at 4, goes to 5, goes to 6, goes to 0, goes to 1, etc. So it’s Tail = Tail + 1, But… Tail = (Tail + 1) % 7 So Tail % MaxSize works as follows: 4 % MaxSize = 4 5 % MaxSize = 5 6 % MaxSize = 6 7 % MaxSize = 0 8 % MaxSize = 1
32
Circular Queues We will look at implementing the following modules:
IsFull() Check if the queue is full IsEmpty() AddToQ(N) Add a new item (N) to the back of the queue DeleteFromQ() Remove the front value from the queue ClearQ() Empty the queue
33
Circular Queues Tail Head IsFull() 1 41 2 59 26 3 4 7
if Head = (Tail % MaxSize) + 1 If they have caught up to each other, then it’s full. Tail 7 17 81 6 1 54 43 1 41 2 59 26 3 4 Head 68 35 5 2 22 99 4 3
34
Circular Queues Tail MaxSize Head IsFull() 81 1 35 2 99 22 3 4 68 54 5
if Head = (Tail % MaxSize) + 1 If they have caught up to each other, then it’s full. Tail Head 81 1 35 2 99 22 3 4 68 54 5 6 17 MaxSize
35
Circular Queues MODULE IsFull(): Boolean Full; IF Head = (Tail + 1) % MaxSize THEN Full <- True; ELSE Full <- False; ENDIF; RETURN Full; END.
36
Circular Queues Or MODULE IsFull():
RETURN Head = (Tail + 1) % MaxSize; END.
37
Circular Queues Tail Head IsEmpty() 1 41 59 2 26 3 4 if Tail = Head 7
6 1 1 41 59 2 26 3 4 5 2 4 3
38
Circular Queues Queue MaxSize Head Tail IsEmpty() 1 2 3 4 5 6
if Tail = Head Head Tail 1 2 3 4 5 6 Queue MaxSize
39
Circular Queues MODULE IsEmpty(): Boolean Empty; IF Head = Tail THEN Empty <- True; ELSE Empty <- False; ENDIF; RETURN Empty; END.
40
Circular Queues Or MODULE IsEmpty(): RETURN Head = Tail; END.
41
Circular Queues AddToQ(17) 7 6 1 Tail 54 Head 68 35 5 2 22 99 4 3
42
Circular Queues AddToQ(81) Tail 7 17 6 1 54 Head 68 35 5 2 22 99 4 3
43
Circular Queues Tail Head AddToQ(43) 7 17 81 6 1 54 68 35 5 2 22 99 4
17 81 6 1 54 Head 68 35 5 2 22 99 4 3
44
Circular Queues Tail Head Queue is Full! 7 17 81 6 1 43 54 68 35 5 2
17 81 6 1 54 43 Head 68 35 5 2 22 99 4 3
45
Circular Queues Queue Head Tail MaxSize AddToQ(N) 31 1 41 2 59 3 26 53
Increment the Tail pointer % MaxSize, and add to the Tail. Head Tail 31 1 41 2 59 3 26 53 4 5 6 Queue MaxSize
46
Circular Queues MODULE AddToQ(N): IF IsFull() = True THEN Print “Queue is Full”; ELSE Tail <- (Tail + 1) % MaxSize; Queue[Tail] <- N; ENDIF; END.
47
Circular Queues Head Tail DeleteFromQ() 7 43 6 1 12 54 68 35 5 2 22 99
43 6 1 Tail 12 54 68 35 5 2 22 99 4 3
48
Circular Queues Head Tail DeleteFromQ() 7 6 1 12 54 68 35 5 2 22 99 4
6 1 Tail 12 54 68 35 5 2 22 99 4 3
49
Circular Queues DeleteFromQ() 7 6 1 Tail 54 Head 68 35 5 2 22 99 4 3
50
Circular Queues Queue Head Tail MaxSize DeleteFromQ() 31 1 41 2 59 3
Write Head value into N, and add one to Head Head Tail 31 1 41 2 59 3 26 53 4 5 6 Queue MaxSize
51
Circular Queues MODULE DeleteFromQ(): N <- 0; IF IsEmpty() = True THEN Print “Queue is Empty”; ELSE N <- Queue[Head]; Head <- (Head + 1) % MaxSize; ENDIF; RETURN N; END.
52
Circular Queues Tail Head ClearQ() 1 41 59 2 26 3 4
Set Tail = Head = 0 Tail Head 7 6 1 1 41 59 2 26 3 4 5 2 4 3
53
Circular Queues Queue MaxSize Head Tail ClearQ() 1 2 3 4 5 6
Set Tail = Head = -1 Head Tail 1 2 3 4 5 6 Queue MaxSize
54
Circular Queues MODULE ClearQ(): Tail <- -1; Head <- Tail; END.
55
etc.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.