Circular Queue
Circular Queue
2
Queue Operations
3
Array implementation of queues
• A queue is a first in, first out (FIFO) data structure
• This is accomplished by inserting at one end (the rear) and
deleting from the other (the front)
0 1 2 3 4 5 6
myQueue: 7
17 23 97 44
front = 0 rear = 3
front = 0 rear = 3
Initial queue: 17 23 97 44
front = 1 rear = 4
• Notice how the array contents “crawl” to the right as elements are inserted and
deleted
• This will be a problem after a while!
5
Enqueue()
• Inserting 15 • The queue insert operation is
known as enqueue.
Rear
0 1 2 3 4 5 6
7
Front
Front=0 Rear=0
6
Enqueue()
• Inserting 23
Rear
0 1 2 3 4 5 6
7
15
Front
Front= 0 Rear= 1
7
Enqueue()
• Inserting 44
Rear
0 1 2 3 4 5 6
7
15 23
Front
Front= 0 Rear= 2
8
Enqueue()
• Inserting 56
Rear
0 1 2 3 4 5 6
7
15 23 44
Front
Front= 0 Rear= 3
9
Enqueue()
• Inserting 63
Rear
0 1 2 3 4 5 6
7
15 23 44 56
Front
Front= 0 Rear= 4
10
Enqueue()
• Inserting 70
Rear
0 1 2 3 4 5 6
7
15 23 44 56 63
Front
Front= 0 Rear= 5
11
Enqueue()
• Inserting 87
Rear
0 1 2 3 4 5 6
7
15 23 44 56 63 70
Front
Front= 0 Rear= 6
12
Enqueue()
• Inserting 99
Rear
0 1 2 3 4 5 6
7
15 23 44 56 63 70 87
Front
Front= 0 Rear= 7
13
Enqueue()
• Inserting 99
Rear
0 1 2 3 4 5 6
7
15 23 44 56 63 70 87 99
Front
Front= 0 Rear= 8,
Queue Full
14
Dequeue()
• The queue delete operation is
• Deleting 15 known as dequeue.
Rear
0 1 2 3 4 5 6
7
15 23 44 56 63 70 87 99
Front
Front= 0 Rear= 8
15
Dequeue()
• Deleting 23
Rear
0 1 2 3 4 5 6
7
23 44 56 63 70 87 99
Front
Front= 1 Rear= 8
16
Dequeue()
• Deleting 44
Rear
0 1 2 3 4 5 6
7
44 56 63 70 87 99
Front
Front= 2 Rear= 8
17
Dequeue()
• Deleting 56
Rear
0 1 2 3 4 5 6
7
56 63 70 87 99
Front
Front= 3 Rear= 8
18
Dequeue()
• Deleting 63
Rear
0 1 2 3 4 5 6
7
63 70 87 99
Front
Front= 4 Rear= 8
19
Dequeue()
• Deleting 70
Rear
0 1 2 3 4 5 6
7
70 87 99
Front
Front= 5 Rear= 8
20
Dequeue()
• Deleting 87
Rear
0 1 2 3 4 5 6
7
87 99
Front
Front= 6 Rear= 8
21
Dequeue()
• Deleting 99
Rear
0 1 2 3 4 5 6
7
99
Front
Front= 7 Rear= 8
22
Dequeue()
Rear
0 1 2 3 4 5 6
7
Front
Front= 8 Rear= 8
23
IsEmpty()
• Empty Queue
0 1 2 3 4 5 6
7
24
Circular arrays
• We can treat the array holding the queue elements as
circular (joined at the ends)
0 1 2 3 4 5 6
myQueue: 7
44 55 11 22 33
rear = 1 front = 5
5 15 2
78 26
Rear
4 3 Here ,
Rear= 5 & Front = 0
26
Dequeue()
• Deleting 9
7 0 Front
6 1
6
s
Dequeue()
5 15 2
78 26
Rear
4 3 Here ,
Rear= 4 & Front = 1
27
Full and empty queues
• If the queue were to become completely full, it would look
like this:
0 1 2 3 4 5 6
myQueue: 7
44 55 66 77 88 11 22 33
rear = 5 front = 5
0 1 2 3 4 5 6
myQueue: 7
rear = 5 front = 5
This is a problem! 28
Full and empty queues: solutions
• Solution #1: Keep an additional variable
0 1 2 3 4 5 6
myQueue: 7
44 55 66 77 88 11 22 33
0 1 2 3 4 5 6
myQueue: 7
44 55 66 77 11 22 33
rear = 4 front = 5
29
• aPriority Queue
priority queue is an abstract data
type which is like a regular queue or
stack data structure, but where
additionally each element has a
"priority" associated with it.
30
Insertion Operation
1 40 3 50 4 20 NULL
Head Tail
(front) (rear)
31
Insertion Operation
Head
1 40 3 50 4 20 NULL
2 87
Front Rear
(Inserting)
32
Insertion Operation
Head
1 40 2 87 3 20
Front 4 20 NULL
Rear 33
Deletion Operation
1 40 2 87 3 50
Front
4 20 NULL
Rear
34
Deletion Operation
Head
2 87 3 50 4 20 NULL
Front Rear
35
Deques
• A deque is a double-ended queue
• Insertions and deletions can occur at either end
• Implementation is similar to that for queues
• Deques are not heavily used
• You should know what a deque is, but we won’t explore them much
further
36
Insertion at Front
• Create a Node containing data and then point it to the node
which is pointed to the head pointer.
• Then head pointer will point the created node.
40 50 20 NULL
Insert
Front Rear
37
Insertion at Rear
• Create a Node containing data and then point it to the node which is pointed
to the head pointer if head is NULL otherwise , check till the NULL pointer and
then insert at that pointer.
40 50 20 NULL
Front Rear
38
Deletion Operation at Front
• Create a node temp then point it to head .Then head will
point to head->next .
Then we have to free temp.
data add
Delete Head
40 50 20 NULL
Front Rear
39
Deletion Operation at Rear
• If head is NULL then queue is empty.
• If head->next is NULL then head=NULL.
• Otherwise, create a node temp.Then, we will check temp->next->next .If it is
not
NULL then we will set temp= temp->next. Then if we find NULL then we
free(temp->next).
Head data add
40 50 20 NULL
Delete
Front Rear
40
QUEUING THEORY
Queuing Theory is a field of applied
mathematics and computer science that is
used to predict the performance of queues
41
Queuing Theory
• The rate at which customers arrive in the queue for service is known
as the arrival rate. It may be random or regular.
• Service time is the average time required to complete the processing
of a customer request.
43
Queuing Theory
44
Queuing Theory
46
47
Queuing Theory
• The two factors that most affect the performance of queues are the
arrival rate and the service time.
48
4-5 Queue Applications
• Categorizing Data
• Queue Simulation
49
Categorizing Data
50
Categorizing Data
Initial list of numbers:
3 22 12 6 10 34 65 29 9 30 81 4 5 19 20 57 44 99
52
53
54
55
56
57
(Continued)
58