You are on page 1of 28

QUEUES

Intro Movie Hall Ticketing

One Way Traffic


Intro
• Queue is a linear list of elements in which deletion of an element can
take place at one end, called the front and insertion can take place at
the other end, called the rear.

• The first element in a queue will be the first one to be removed from
the list.

• Queues are also called FIFO (First In First Out) i.e. the data item
stored first will be accessed first
Queue Representation
• In queue, we access both ends for different reasons
Applications of queue
• Serving requests on a single shared resource, like a printer, CPU task
scheduling etc.

• In real life, Call Center phone systems will use Queues, to hold people
calling them in an order, until a service representative is free.

• Handling of interrupts in real-time systems. The interrupts are


handled in the same order as they arrive, First come first served.
Operations on Queue
 A queue is a linear data structure.
 It is controlled by two operations: insertion (Enqueue) and deletion
(Dequeue).
 Insertion operation takes place from the rear end of the queue and
deletion operation takes place from the front end of the queue.
 Insertion operation adds an element to the queue.
 Deletion operation removes an element from the queue.
 There are two variables FRONT and REAR. FRONT points to the beginning of
the filled queue and takes care of deletion operation. REAR points to the end
of the queue and takes care of insertion operation.
 These two operations implement the FIFO method .
The queue as an ADT
• A queue q of type T is a finite sequence of elements with the
operations
• MakeEmpty(q): To make q as an empty queue
• IsEmpty(q): To check whether the queue q is empty. Return true if q is empty,
return false otherwise.
• IsFull(q): To check whether the queue q is full. Return true in q is full, return
false otherwise.
• Enqueue(q, x): To insert an item x at the rear of the queue, if and only if q
is not full.
• Dequeue(q): To delete an item from the front of the queue q if and only if q
is not empty.
• Traverse (q): To read entire queue that is display the content of the queue.
Implementation of queue
• There are two techniques for implementing the queue:

• Array implementation of queue (static memory allocation)


• Linear array implementation (Linear Queue)
• Circular array Implementation (Circular queue)

• Linked list implementation of queue (dynamic memoryallocation)


Queue as an Array
Array implementation of Queue uses 4

3
 an array to store data
 an integer type variable usually called the FRONT, 30
2
which points to the beginning of the array and an
2 20 1
integer variable REAR, which points to the end of
the filled array. REAR
10 0

0
FRONT
Empty Queue

front =-1 and rear =-1


Insertion Operation Explained

Insert 50 will result in Overflow


and Queue is full
Insertion Operation
 Initially when the queue is empty, FRONT and REAR can have any
integer value other than any valid index number of the array. Let
FRONT = -1; REAR=-1;
 The first element in the empty queue goes to the 0th position of the
array and both FRONT and REAR are initialized to value 0.
 After this every insertion operation increase the REAR by 1 and
inserts new data at that particular position.
 As arrays are fixed in length, elements can not be inserted beyond
the maximum size of the array. Pushing data beyond the maximum
size of the queue (i.e. when REAR = MAX SIZE -1) results in “data
overflow”.
Implementation of enqueue()
int enqueue(int data) {
if(rear == MAX-1) //overflow
return 0;
else {
if (front== -1)
front=0;
rear = rear + 1;
queue[rear] = data;
return 1;
}
}
DELETION OPERATION EXPLAINED
Delete Operation
The delete operation deletes the very first item from the queue.
Any attempt to delete an element from the empty queue (when
FRONT,REAR=-1) results in “data underflow” condition.
Each time the deletion operation is performed, the FRONT
variable is decremented by 1.
When there is only one element in the queue, deletion operation of
that element makes the queue empty and both FRONT and REAR will
be assigned the value -1.
Implementation of dequeue()
int dequeue() {
if( front == -1) {
// underflow
return 0;
}
int data = queue[front];
if (front == rear)
front = rear =-1;
else
front = front + 1;
return data;
}
Problems with Linear queue implementation
• Both rear and front indices are increased but never decreased.

• As items are removed from the queue, the storage space at the
beginning of the array is discarded and never used again.

• Wastage of the space is the main problem with linear queue which is
illustrated by the following example.
Circular queue
• A queue where the start and end of the queue are joined together.

• A circular queue is one in which the insertion of a new element is done


at very first location of the queue if the last location of the queue is
full.
Front

Rear
Rear Rear
Front

Front
Front

Rear

Rear

Front
1. In a circular queue, if rear reaches to MAX-1 index, then set rear to 0 i.e. 0th index.
2. In a circular queue, if front reaches to MAX-1 index, then set front to 0 i.e. 0th index.
3. At any time, the position of element to be inserted is calculated by

rear = (rear + 1) % MAX

4. After deleting an element from circular queue, the position of front end is calculated by

front = (front +1) % MAX

5. Circular queue is full when


(rear +1) % MAX ==front

or

front == 0 && rear – MAX-1 || front == rear +1


Insertion in Circular Queue (Enqueue)
Deletion in Circular Queue (Dequeue)
Display Circular Queue
Circular queue
isFull() IsEmpty
- If front == (rear % MAX) + 1 - If front == rear
Then Full <- True; Then Full <- True;
Else Full <- False; Else Full <- False;

- If they have caught up to each other, then it’s full


Circular queue
Enqueue operation Dequeue operation
Step 1. start Step 1. start
Step 2. if (front == (rear+1)%max) Step 2. if isEmpty() == True
Print error “circular queue overflow “ Print error “Queue is Empty“
Step 3. else{ Step 3. else{
rear = (rear+1)%max element = Q[front]
Q[rear] = element; front = (front + 1) % max
} }
Step 4. stop Step 4. stop

You might also like