DS - UNIT 2 Queue
DS - UNIT 2 Queue
QUEUE
Queue is defined as an ordered collection of items from which, the items may be deleted at one
end called FRONT end and the items may be inserted at the other end called REAR end.
Queue works on FIFO (First In First Out) fashion.
Classification of Queue
Queue
Linear Queue
It is also known as sequential queue. It is implemented using linear array or linear list. This
linear queue is represented using linear array with 2 variables FRONT & REAR.
0 1 2 3 4 5 6 7
26 9 23 34 45 67 45 34
FRONT REAR
Deletion Insertion
Queue ADT
A collection of queue data members and queue operations that preform on queue data. The data
members or variables are FRONT and REAR. Queue operations such as
1. Create()- Create queue with n elements.
2. Insert()- insert an element at the end of the queue
3. Delete()- delete an element from the beginning of the queue.
4. Isempty()- returns true if the queue is empty otherwise false
5. Traverse()- accessing all the elements in the queue.
6. Count()- count the number of elements in the queue.
Insertion()
This operation inserts an element on the end of a queue. If queue is full, insertion is not possible.
Algorithm: Qinsert(Q, FRONT, REAR, N, item)
Step 1: check overflow
if REAR= = N-1 then
print “Overflow”
return
Step 2: Increment REAR pointer
REAR= REAR+1
Step 3: Insert the item
Q[REAR]= item
Step 4: Return
Explanation:
Consider a queue contain 5 elements and Max is 8
0 1 2 3 4 5 6 7
26 9 23 34 45
FRONT REAR
1. Check queue overflow condition if it is print overflow. Here, REAR=4 so it is false.
2. Increment REAR=REAR + 1
0 1 2 3 4 5 6 7
26 9 23 34 45
FRONT REAR
3. Insert the item
Q[REAR]= item i.e., Q[5]=86
0 1 2 3 4 5 6 7
26 9 23 34 45 86
FRONT REAR
4. Return
The following C function illustrates Queue insertion
void Qinsert()
{
if(REAR == N-1)
printf("\n Queue Overflow");
else
{
printf("\n Enter an item:");
scanf("%d", &item);
REAR++;
QUEUE[REAR]= item;
}
}
Deletion
This operation deletes an item from the beginning of the queue. If Queue is Empty, deletion
operation is impossible.
Algorithm: Qdelete(Q, FRONT, REAR, N, item)
Step 1: Check underflow
if REAR= = FRONT-1 then
print "Underflow"
return
Step 2: Delete the item
item = Q[FRONT]
Step 3: if FRONT= = REAR
FRONT=0, REAR=-1
else
FRONT=FRONT+1
`Step 4: return
Explanation
Consider a queue contain 5 elements and Max is 8
0 1 2 3 4 5 6 7
26 9 23 34 45
FRONT REAR
1. Check queue is underflow. Here, FRONT=0 and REAR=4, so it is false
2. Delete the item. i.e., item= Q [0]⟹item =26
0 1 2 3 4 5 6 7
9 23 34 45
FRONT REAR
3. Check FRONT and REAR value is equal. Here FRONT=0 and REAR =4. So, it is
false, increment FRONT=FRONT+1
0 1 2 3 4 5 6 7
9 23 34 45
FRONT REAR
4. Return
The following C function illustrates Queue deletion
void Qdelete()
{
if (REAR= = FRONT - 1)
printf("\nQueue Underflow");
else if(REAR= = FRONT)
{
printf("This is the last element in the Queue");
printf("\n The Last element deleted is : %d", QUEUE[FRONT]);
FRONT=0;
REAR=-1;
}
else
{
printf("\n Deleted item is %d", QUEUE[FRONT]);
FRONT++;
}
}
Qempty Operation
This operation returns true if the queue is empty otherwise false.
A Queue is empty only when REAR=-1 and FRONT=0
The following function check Queue is empty
int Qempty()
{
if(REAR= = FRONT-1)
return 1;
else
return 0;
}
Qfull Operation
This operation returns true, if the queue is full otherwise false. A Queue is full only when
REAR== N-1. The following function check Queue is full.
int Qfull()
{
if(REAR= = N-1)
return 1;
else
return 0;
}
Traversal operation
This operation access all the elements in the queue.
Algorithm: Qdisplay(Q, FRONT, REAR, N)
Step 1: Check underflow
if REAR== FRONT-1 then
print "Underflow"
return
Step 2: Repeatedly access the queue elements
for (i=FRONT; i<= REAR; i++)
print Q[i];
Step 3: return
Explanation
Consider the following Queue Max Size is 8 and it contain 5 elements.
0 1 2 3 4 5 6 7
26 9 23 34 45
FRONT REAR
1. Check underflow, here REAR=4, FRONT=0 so it is false therefore Go to step 2
2. Repeatedly print the value of Queue 26 9 23 34 45
3. Return
Linked Representation of Queues
In linked list implementation, every queue element has two parts, one that stores the
data and the other that stores the address of the next element.
The FRONT pointer points the first element and the REAR pointer points the last
element.
All insertions will be done at the REAR end and all insertions at the FRONT end
FRONT = REAR = NULL indicates the queue is empty
Insert Operation
The insert operation is used to insert an element into a queue. The new element is added as
the last element of the queue.
Algorithm: QINSERT()
Step 1: Allocate memory for the new node and name it as NEW_NODE
Step 2: Set NEW_NODE → INFO=item
Step 3: If FRONT = NULL
Set FRONT=REAR=NEW_NODE
Set FRONT→ LINK = REAR→ LINK =NULL
Else
Set REAR→> LINK=NEW_NODE
Set REAR = NEW_NODE
Set REAR→NEXT=NULL
End if
Step 4: END
Consider the following figure shows linked queue.
FRONT REAR
To insert an element with value 80, we first allocate memory for a new node, store the value
80 in its INFO part and NULL in its LINK part. We add the new node at the end of the linked
stack. The updated linked queue is shown below:
FRONT REAR
Delete Operation
The delete operation is used to delete the FRONT element from the queue.
Algorithm QDELETE()
Step 1: If FRONT = NULL
Print "Underflow"
goto Step 5
Step 2: Set PTR = FRONT
Step 3: Set FRONT = FRONT → LINK
Step 4: Free PTR
Step 5 End
Consider the linked Queue as shown below:
FRONT REAR
Before deleting the values, we first check FRONT = = NULL if so, the queue is empty
otherwise it deletes a value from a queue.
FRONT REAR
CIRCULAR QUEUE
Circular Queue is a linear data structure in which the operations are performed based on FIFO
(First In First Out) principle and the last position is connected back to the first position to make
a circle. It is also called ‘Ring Buffer’. The representation of circular queue is shown below:
Insert Operation
This operation inserts an element on the REAR position of a queue. If queue is full, insertion
is not possible.
Algorithm CQINSERT(Q,FRONT,REAR,N,ITEM)
Step 1: If FRONT == (REAR+1) %N
display “overflow”
Step 2: IF FRONT == -1
FRONT = REAR = 0
Step 3: ELSE REAR = (REAR + 1)%N
Step 4: Q[REAR] = ITEM
Step 5: END
Delete Operation
This operation deletes an item from the FRONT position of the queue. If Queue is Empty,
deletion operation is impossible.
Algorithm QDELETE (Q, FRONT, REAR, N, ITEM)
Step 1: If FRONT == -1
Write “underflow”
Return
Step 2: ITEM = Q[FRONT]
Step 3: IF FRONT = REAR
FRONT = REAR = -1
ELSE
FRONT = (FRONT + 1)%N
Step 4 : END
The following figure illustrates insertion and deletion of a queue
PRIORITY QUEUE
A priority queue is a queue such that element of the queue has been assigned with a
priority such that the order in which elements are processed comes from the following
rules:
o An element of higher priority is processed before any element of lower
priority
o If two elements have same priority, then the element which come first will be
processed first.
Types of priority queue
There are two types of priority queue. They are:
Priority Queue
In Deque or Double Ended Queue, insertion and deletion can be done from both ends of
the queue either from the front or rear.
Types of DEQUE
The 2 variations of deque is as follows:
Operations on Deque:
The basic operations are:
insertFront(): Adds an item at the front of Deque
insertLast(): Adds an item at the rear of Deque
deleteFront(): Deletes an item from front of Deque.
deleteLast(): Deletes an item from rear of Deque.
Consider an input restricted deque and now we can add element only on the REAR side of the
queue but we can delete from both sides.
Applications of Queues:
Queues are used as waiting lists for a single shared resource like printer, disk, CPU.
Queues are used as buffers in most of the applications like MP3 media player, CD
player, etc.
Queue are used to maintain the play list in media players in order to add and remove
the songs from the play-list.
Queues are used in operating systems for handling interrupts.