0% found this document useful (0 votes)
12 views58 pages

Circular Queue

Uploaded by

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

Circular Queue

Uploaded by

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

CSE 225

Queue, Deque and Priority Queue


Queues, and Deques

• A queue is a first in, first out (FIFO) data structure


• Items are removed from a queue in the same order as they were inserted

• A deque is a double-ended queue—items can be inserted and


removed at either end

2
Queue Operations

• Enqueue() : Add an element to the rear


of the queue.
• Dequeue() : Remove and returns the
element at the front of the
queue.
• isEmpty() : returns true if queue is empty.
• isFull() : returns true if queue is
full.

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

• To insert: put new element in location 4, and set rear to 4


• To delete: take element from location 0, and set front to 1
4
Array implementation of queues

front = 0 rear = 3

Initial queue: 17 23 97 44

After insertion: 17 23 97 44 333

After deletion: 23 97 44 333

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

 When, Front = Rear, it is an empty


queue.

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

• Elements were added to this queue in the order 11, 22,


33, 44, 55, and will be removed in the same order
• Use: front = (front + 1) % myQueue.length;
and: rear = (rear + 1) % myQueue.length;
25
Enqueue()
• Inserting 9 ,6 ,15 ,26 ,78
Front
7 0
9
6 1
6
s
Enqueue()

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

• If we were then to remove all eight elements, making the queue


completely empty, it would look like this:

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

count = 8 rear = 5 front = 5

• Solution #2: (Slightly more efficient) Keep a gap between


elements: consider the queue full when it has n-1 elements

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.

• In a priority queue, an element with high


priority is served before an element with low
priority. If two elements have the same
priority, they are served according to their
order in the queue.

30
Insertion Operation

prio data add


Head

1 40 3 50 4 20 NULL

Head Tail
(front) (rear)

31
Insertion Operation

• Inserting 87 with priority 2

Head

1 40 3 50 4 20 NULL

2 87
Front Rear
(Inserting)

32
Insertion Operation

• Inserting 87 with priority 2 2 87

Head

1 40 2 87 3 20

Front 4 20 NULL

Data will be inserted

Rear 33
Deletion Operation

• Normally Deletion operation occurs at head


pointer
Head

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.

Head data add

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.

Head data add Insert

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

• A single-server queue can provide service to only


one customer at a time (hot food vendor on street
corner).
• A multi-server queue can provide service to many
customers at a time (bank, post-office).
• Multiqueues – multiple single-server queues (a
grocery store).
• A customer is any person or thing needing the
service.
• The service is any activity needed to accomplish the
required result.
42
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

• In an ideal situation, customers would arrive at a


rate that matches the service time.
• However, things are seldom ideal. Sometimes the
server can be idle because there are no customers
to be served. At other times there will be many
customers to be served.
• If we can predict the patterns, we may be able to
minimize idle servers and waiting customers.

44
Queuing Theory

• One of the main tasks in Queuing Theory is to


predict such patterns.
• Specifically, it attempts to predict queue time
(which is defined as the average length of time
customers wait in the queue), the average size of
the queue, and the maximum queue size.
• These predictions are based on the two factors: the
arrival rate and the average service time (the
average of the total service time between idle
periods)
45
Queuing Theory

• Given queue time and service time, we know response time, - a


measure of the average time from the point at which customers
enter the queue until the moment they leave the server.

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

We develop two queue applications. The first shows how to use a


queue to categorize data. The second is a queue simulator,
which is an excellent tool to simulate the performance and to
increase our understanding of its operation.

• Categorizing Data
• Queue Simulation

49
Categorizing Data

• Categorizing Data is the rearrangement of data without destroying


their basic sequence.
• As an example: suppose, it is necessary to rearrange a list of numbers
grouping them, while maintaining original order in each group
(multiple-queue application).

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

We need to categorize them into four different groups:


Group 1: less than 10
Group 2: between 10 and 19
Group 3: between 20 and 29
Group4: 30 and greater
| 3 6 9 4 5 | 12 10 19 | 22 29 20 | 34 65 30 81 57 44 99
51
Categorizing Data

• The solution: we build a queue for each of the four categories. We


than store the numbers in the appropriate queue as we read them.
• After all the data have been processed, we print each queue to
demonstrate that we categorized data correctly.

52
53
54
55
56
57
(Continued)

58

You might also like