0% found this document useful (0 votes)
9 views22 pages

08 Queues

The document provides an overview of queues, describing their structure, operations, and types, including linear, circular, double-ended, and priority queues. It explains the basic operations of enqueue and dequeue, and details the implementation of circular queues and double-ended queues. Additionally, it outlines the rules governing priority queues, emphasizing the importance of element priority in processing order.

Uploaded by

Neha Kumari
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)
9 views22 pages

08 Queues

The document provides an overview of queues, describing their structure, operations, and types, including linear, circular, double-ended, and priority queues. It explains the basic operations of enqueue and dequeue, and details the implementation of circular queues and double-ended queues. Additionally, it outlines the rules governing priority queues, emphasizing the importance of element priority in processing order.

Uploaded by

Neha Kumari
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/ 22

Queues

Introduction to Queues
• A queue is a waiting line – seen in daily life

– A line of people waiting for a bank teller


– A line of cars at a toll both

• What other kinds of queues can you think of

The queue has a front and a rear.

$ $

Front Rear
In Queue
a. Items can be removed only at the front
b. Items can be added only at the other end, the back

1 2 3 Original Queue

Front Rear

Add 4 to the Queue 1 2 3 4

Front Rear

2 3 4
Remove the element
from the Queue
Front Rear
Types Of Queues

• Linear Queue
• Circular Queue
• Double Ended Queue (Deque)
• Priority Queue
Double Ended Queue
Double ended queues, called deques for short, are a generalized form
of the queue. It is exactly like a queue except that elements can be
added to or removed from the head or the tail.
However, no element can be added and deleted from the middle.

In the computer’s memory, a deque is implemented using either a


circular array or a circular doubly linked list.

In a deque, two pointers are maintained, LEFT and RIGHT, which


point to either end of the deque. The elements in a deque extend from
the LEFT end to the RIGHT end and since it is circular, Dequeue[N–1]
is followed by Dequeue[0].
There are two variants of a double-ended queue. They
include :
• Input restricted deque In this, insertions can be done
only at one of the ends, while deletions can be done from
both ends.
• Output restricted deque In this deletions can be done only
at one of the ends, while insertions can be done on both
ends.
Priority Queues
A priority queue is a data structure in which each element is assigned
a priority. The priority of the element will be used to determine the
order in which the elements will be processed.

The general rules of processing the elements of a priority queue are


• An element with higher priority is processed before an element
with a lower priority.
• Two elements with the same priority are processed on a first-
come-first-served (FCFS) basis.
The Queue As an ADT
1. A queue is a sequence of data elements
2. Basic operations
a. Enqueue (add element to back)
b. Dequeue (remove element from front)

Enqueue & Dequeue operations


Array Implementation -Dequeue
When an item is taken from the queue, it always comes from the front.
This a dequeue operation.
Array Implementation -Enqueue
When an item is inserted into the queue, it always goes at the end (rear).
This a enqueue operation.
LINKED REPRESENTATION OF QUEUEs
Enqueue:
Dequeue
Circular Queue
Drawback of Linear Queue
• Once the queue is full, even though few elements from the front are deleted and
some occupied space is relieved, it is not possible to add anymore new elements,
as the rear has already reached the Queue’s rear most position.

Circular Queue
• This queue is not linear but circular.

• Its structure can be like the following figure:


• In circular queue, once the Queue is full the

"First" index of the Queue becomes the

"Rear" most index, if and only if the "Front" Figure: Circular Queue having
Rear = 5 and Front = 0
element has moved forward. otherwise it will be

a "Queue overflow" state.

09/10/08 14
Algorithms for Insert Operations in Circular Queue
For Insert Operation
Insert-Circular-Q(CQueue, Rear, Front, N, Item)
Here, CQueue is a circular queue.
Rear represents the location in which the data element is to be inserted and
Front represents the location from which the data element is to be removed.
N is the maximum size of CQueue and
Item is the new item to be added.
Initailly Rear = -1 and Front = -1.
1. If Front = -1 and Rear = -1 then Set Front :=0 and go to step 4.
2. If Front =0 and Rear = N-1 or Front = Rear + 1
then Print: “Circular Queue Overflow” and Return.
3. If Rear = N -1 then Set Rear := 0 and go to step 4.
4. Set CQueue [Rear] := Item. and Rear:=Rear + 1
5. Return
09/10/08 15
For Delete Operation
Delete-Circular-Q(CQueue, Front, Rear, Item)
CQueue is the place where data are stored.
Rear represents the location in which the data element is to be inserted and
Front represents the location from which the data element is to be removed.
Front element is assigned to Item.
Initially, Front = -1.
*..Delete without Insertion

1. If Front = -1 then Print: “Circular Queue Underflow” and Return.


2. Set Item := CQueue [Front]
3. If Front = N – 1 then Set Front = 0 and Return.
4. If Front = Rear then Set Front = Rear = -1 and Return.
5. Set Front := Front + 1
6. Return.

09/10/08 16
Initailly Rear = -1 and Front = -1.

Example- ENQUEUE 1. If Front = -1 and Rear = -1 then Set Front :=0 and go to step 4.
2. If Front =0 and Rear = N-1 or Front = Rear + 1
Circular queue with N = 5. then Print: “Circular Queue Overflow” and Return.
3. If Rear = N -1 then Set Rear := 0 and go to step 4.
4. Set CQueue [Rear] := Item. and Rear:=Rear + 1
5. Return
1. Initially, Rear = 0, Front = 0. 4. Insert 20, Rear = 3, Front = 0.
Front

Rear
Rear

2. Insert 10, Rear = 1, Front = 1. 5. Insert 70, Rear = 4, Front = 1.


Front
Front

Rear
3. Insert 50, Rear = 2, Front = 1. 6. Delete front, Rear = 4, Front = 2.
Front Rear Front

09/10/08 17
Rear
Initailly Rear = -1 and Front = -1.

Example- ENQUEUE 1. If Front = -1 and Rear = -1 then Set Front :=0 and go to step 4.
2. If Front =0 and Rear = N-1 or Front = Rear + 1
Circular queue with N = 5. then Print: “Circular Queue Overflow” and Return.
3. If Rear = N -1 then Set Rear := 0 and go to step 4.
4. Set CQueue [Rear] := Item. and Rear:=Rear + 1
5. Return
1. Initially, Rear = -1, Front =-1 4. Insert 20, Rear = 2, Front = 0.
Front

Rear
Rear

2. Insert 10, Rear = 0, Front = 0. 5. Insert 70, Rear = 3, Front = 0.


Front
Front

Rear
3. Insert 50, Rear = 1, Front = 0. 6. Delete front, Rear = 3, Front =1.
Front Rear Front

09/10/08 18
Rear
Initially, Front = -1. Initailly Rear = -1 and Front = -1.
1. If Front = -1 then Print: “Circular Queue Underflow” and Return . 1. If Front = -1 and Rear = -1 then Set Front :=0 and go to step 4.
2. If Front =0 and Rear = N-1 or Front = Rear + 1
2. Set Item := CQueue [Front]
then Print: “Circular Queue Overflow” and Return.
3. If Front = N – 1 then Set Front = 0 and Return.
3. If Rear = N -1 then Set Rear := 0 and go to step 4.
4. If Front = Rear then Set Front = Rear = -1 and Return. 4. Set CQueue [Rear] := Item. and Rear:=Rear + 1
5. Set Front := Front + 1 5. Return
6. Return.
7. Insert 100, Rear = 4, Front = 1. 10. Delete front, Rear = 0, Front = 2.
Front Rear

ENQUEUE/DEQUEUE
Circular queue with N = 5. Front
Rear

8. Insert 40, Rear = 0, Front = 1. 11. Delete front, Rear = 0, Front = 3.


Rear
Rear Front

Front

9. Insert 140, Rear = 0, Front = 1. 12. Delete front, Rear = 0, Front = 4.


Rear
As Front = Rear + 1, so Queue overflow.
Rear Front

Front

09/10/08 19
Double Ended Queue
Double ended queues, called deques for short, are a generalized form
of the queue. It is exactly like a queue except that elements can be
added to or removed from the head or the tail.
However, no element can be added and deleted from the middle.

In the computer’s memory, a deque is implemented using either a


circular array or a circular doubly linked list.

In a deque, two pointers are maintained, LEFT and RIGHT, which


point to either end of the deque. The elements in a deque extend from
the LEFT end to the RIGHT end and since it is circular, Dequeue[N–1]
is followed by Dequeue[0].
There are two variants of a double-ended queue. They
include :
• Input restricted deque In this, insertions can be done
only at one of the ends, while deletions can be done from
both ends.
• Output restricted deque In this deletions can be done only
at one of the ends, while insertions can be done on both
ends.
Priority Queues
A priority queue is a data structure in which each element is assigned
a priority. The priority of the element will be used to determine the
order in which the elements will be processed.

The general rules of processing the elements of a priority queue are


• An element with higher priority is processed before an element
with a lower priority.
• Two elements with the same priority are processed on a first-
come-first-served (FCFS) basis.

You might also like