0% found this document useful (0 votes)
15 views

Queue

Queue concept in DSA.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Queue

Queue concept in DSA.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Queue

A queue is a FIFO (First In First Out) or LILO (Last In Last Out) data structure in
which items are inserted at one end called REAR end or the tail of the queue
whereas items are deleted from another end called FRONT end or the head of the
queue.

In other words, it can be defined as a list or a collection with a constraint that the
insertion can be performed at one end called the rear end or tail of the queue and
deletion is performed on another end called the front end or the head of the queue.

A queue is used in such a situation when we want to retrieve items in the same
order as they were inserted.

Standard Queue Operations


The following are some common operations implemented on the queue:

o Enqueue(): When we insert an item into the queue then the operation is
known as enqueue. If the queue is full then the overflow error occurs.
o Dequeue(): When we delete an item from the queue, the operation is known
as dequeue. If the queue is empty than the underflow error occurs.
o isFull(): It determines whether the queue is full or not.
o isEmpty(): It determines whether the queue is empty or not.
o Peek(): It returns the front item of queue without removing it.
o Display(): It prints all the items available in the queue.
A Queue can be represented as a container opened from both the sides in
which the element can be enqueued from one side and dequeued from
another side

Applications of Queue
Due to the fact that queue performs actions on first in first out basis which is quite
fair for the ordering of actions. There are various applications of queues discussed
as below.

 Lines at a ticket counter or any other first come first served scenario
requires a queue.
 Queues are widely used as waiting lists for a single shared resource like
printer, disk, CPU.
 Queues are used in asynchronous transfer of data (where data is not
being transferred at the same rate between two processes) for ex. pipes,
file IO, sockets.

Types of Queue

There are four types of Queues

1. Linear or Ordinary Queue


2. Circular Queue
3. Double Ended Queue (DEQue)
4. Priority Queue

Insertion in Linear Queue


Algorithm

ENQUEUE(queue,front,rear,item,MAXSIZE)

Step1: [Is queue already full?]


If rear=MAXSIZE-1 then
write “Queue overflow error”
return
[End of if structure]
Step 2: [Finding value of rear]

If front = -1 then

set front = 0 and rear = 0

else

set rear = rear+1

[End of If structure]

Step 3: [Insert item at rear end of queue]


set queue[rear]=item
Step 4: return

Deletion from Linear Queue


Algorithm

DEQUEUE(queue,front,rear)

Step1: [Is queue already empty?]


If front = -1 then
write “Queue underflow error”
return
[End of if structure]
Step 2: [Store front item of queue]

set delitem = queue[front]

Step 3: [Find new value of front]


If front=rear then
set front = -1 and rear = -1
else
set front = front+1
[End of If structure]
Step 4: return delitem

/*A PROGRAM TO PERFORM OPERATIONS ON LINEAR QUEUE*/


#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 5

int queue[MAXSIZE];
int front=-1,rear=-1;

int isFULL(void)
{
if(rear==MAXSIZE-1)
return 1;
return 0;
}

int isEMPTY(void)
{
if(front==-1)
return 1;
return 0;
}

void ENQUEUE(int item)


{
if(isFULL())
printf("\nQueue Overflow error.");
else
{
if(isEMPTY())
front=rear=0;
else
rear=rear+1;
queue[rear]=item;
printf("\n%d is inserted in Queue",item);
}

int DEQUEUE(void)
{
int delitem;

if(isEMPTY())
{
printf("Queue underflow error.");
return -1;
}

delitem=queue[front];
if(front==rear)
front=rear=-1;
else
front=front+1;

return delitem;
}

int PEEK(void)
{
if(isEMPTY())
{
printf("Queue underflow error.");
return -1;
}
return queue[front];
}

void DISPLAY(void)
{
int i;

if(isEMPTY())
printf("Queue is empty.");
else
{
printf("Items of Queue are\n");
for(i=front;i<=rear;i++)
printf("%d\t",queue[i]);
}
}

int main()
{
int item,choice;

while(1)
{

printf("\n\t\t\t_ _ _ _ OPERATIONS ON LINEAR QUEUE _ _ _ _\n");


printf("\n\n\t\t\t1. INSERTION");
printf("\n\n\t\t\t2. DELETION");
printf("\n\n\t\t\t3. PEEK");
printf("\n\n\t\t\t4. DISPLAY");
printf("\n\n\t\t\t5. EXIT");
printf("\n\nEnter your choice:");
scanf("%d",&choice);

switch(choice)
{
case 1:
printf("\nEnter the item:");
scanf("%d",&item);
ENQUEUE(item);
break;

case 2:
item=DEQUEUE();
if(item!=-1)
printf("\n%d is deleted from Queue",item);
break;

case 3:
item=PEEK();
if(item!=-1)
printf("\nItem at Front of Queue = %d",item);
break;

case 4:
DISPLAY();
break;
case 5:
printf("\n\n\t\t\t_ _ _ Good Bye _ _ _");
exit(0);

default:
printf("\nWrong choice.");
}
}
return 0;
}

Disadvantage of Linear Queue


When we insert an item into a linear queue, rear is incremented by 1. Once the rear
is set to MAXSIZE-1, we say that queue is full and hence no more items can be
inserted.

If we delete some items from queue then rear still remains set at MAXSIZE-1 so we
can’t insert more items even though the space is available in linear queue.
Insertion in Circular Queue
Algorithm

ENQUEUE(queue,front,rear,item,MAXSIZE)

Step1: [Is queue already full?]


If front = 0 and rear = MAXSIZE-1 OR front = rear+1 then
write “Queue overflow error”
return
[End of if structure]
Step 2: [Finding value of rear]

If front = -1 then

set front = 0 and rear = 0

else

set rear = (rear+1)%MAXSIZE

[End of If structure]

Step 3: [Insert item at rear end of queue]


set queue[rear]=item
Step 4: return

Deletion from Circular Queue


Algorithm

DEQUEUE(queue,front,rear,MAXSIZE)

Step1: [Is queue already empty?]


If front = -1 then
write “Queue underflow error”
return
[End of if structure]
Step 2: [Store front item of queue]

set delitem = queue[front]

Step 3: [Find new value of front]


If front=rear then
set front = -1 and rear = -1
else
set front = (front+1)%MAXSIZE
[End of If structure]
Step 4: return delitem
/*A PROGRAM TO PERFORM OPERATIONS ON CIRCULAR QUEUE*/

#include<stdio.h>

#include<stdlib.h>

#define MAXSIZE 5

int queue[MAXSIZE];

int front=-1,rear=-1;

int isFULL(void)

if((front==0&&rear==MAXSIZE-1)||(front==rear+1))

return 1;

return 0;

int isEMPTY(void)

if(front==-1)

return 1;

return 0;

void ENQUEUE(int item)

{
if(isFULL())

printf("\nQueue Overflow error.");

else

if(isEMPTY())

front=rear=0;

else

rear=(rear+1)%MAXSIZE;

queue[rear]=item;

printf("\n%d is inserted in Queue",item);

int DEQUEUE(void)

int delitem;

if(isEMPTY())

printf("Queue underflow error.");

return -1;

delitem=queue[front];

if(front==rear)

front=rear=-1;
else

front=(front+1)%MAXSIZE;

return delitem;

int PEEK(void)

if(isEMPTY())

printf("Queue underflow error.");

return -1;

return queue[front];

void DISPLAY(void)

int i;

if(isEMPTY())

printf("\nQueue is empty.");

return;

printf("Items of Queue are\n");


if(front<=rear)

for(i=front;i<=rear;i++)

printf("%d\t",queue[i]);

else

for(i=front;i<=MAXSIZE-1;i++)

printf("%d\t",queue[i]);

for(i=0;i<=rear;i++)

printf("%d\t",queue[i]);

int main()

int item,choice;

while(1)

printf("\n\t\t\t_ _ _ _ OPERATIONS ON CIRCULAR QUEUE _ _ _ _\n");

printf("\n\n\t\t\t1. INSERTION");

printf("\n\n\t\t\t2. DELETION");

printf("\n\n\t\t\t3. PEEK");
printf("\n\n\t\t\t4. DISPLAY");

printf("\n\n\t\t\t5. EXIT");

printf("\n\nEnter your choice:");

scanf("%d",&choice);

switch(choice)

case 1:

printf("\nEnter the item:");

scanf("%d",&item);

ENQUEUE(item);

break;

case 2:

item=DEQUEUE();

if(item!=-1)

printf("\n%d is deleted from Queue",item);

break;

case 3:

item=PEEK();

if(item!=-1)

printf("\nItem at Front of Queue = %d",item);

break;

case 4:
DISPLAY();

break;

case 5:

printf("\n\n\t\t\t_ _ _ Good Bye _ _ _");

exit(0);

default:

printf("\nWrong choice.");

return 0;

Double Ended Queue (Deque)


The Deque stands for Double Ended Queue in which the items can be inserted
and deleted from both ends i.e insertion and deletion of items can be performed
from both front and rear end.

Deque can be used both as stack and queue as it allows the insertion and deletion
operations on both ends.

The stack follows the LIFO rule in which both the insertion and deletion can be
performed only from one end. In deque, the insertion and deletion operation can be
performed from one side. Therefore, we conclude that deque can be
considered as a stack.

The queue follows the FIFO rule in which the element is inserted on one end and
deleted from another end. In deque, the insertion can be performed on one end,
and the deletion can be done on another end. Therefore, we conclude that the
deque can also be considered as the queue.

There are two types of Queues, Input-restricted queue, and output-restricted


queue.

1. Input-restricted queue

The input-restricted queue means that some restrictions are applied to the
insertion. In input-restricted queue, the insertion is applied to one end while
the deletion is applied from both the ends.
2. Output-restricted queue
The output-restricted queue means that some restrictions are applied to the
deletion operation. In an output-restricted queue, the deletion can be applied
only from one end, whereas the insertion is possible from both ends.

Operations on Deque
The following operations can be applied on deque:

o Insert at front
o Delete from front
o Insert at rear
o Delete from rear

Other than insertion and deletion, we can also perform peek operation in deque.
Through peek operation, we can get the front and the rear element of the
dequeue.
Applications of Deque
 The deque can be used as a stack and queue; therefore, it can perform
both redo and undo operations.
 It can be used as a palindrome checker means that if we read the string
from both ends, then the string would be the same.

Insertion in Double Ended Queue


Algorithm

ENQUEUE_REAR(queue,front,rear,item,MAXSIZE)

Step1: [Is queue already full?]


If front = 0 and rear = MAXSIZE-1 OR front = rear+1 then
write “Queue overflow error”
return
[End of if structure]
Step 2: [Finding value of rear]

If front = -1 then

set front = 0 and rear = 0

else

set rear = (rear+1)%MAXSIZE

[End of If structure]

Step 3: [Insert item at rear end of queue]


set queue[rear]=item
Step 4: return

ENQUEUE_FRONT(queue,front,rear,item,MAXSIZE)

Step1: [Is queue already full?]


If front = 0 and rear = MAXSIZE-1 OR front = rear+1 then
write “Queue overflow error”
return
[End of if structure]
Step 2: [Finding value of front]

If front = -1 then

set front = 0 and rear = 0


else

set front = (front+MAXSIZE-1)%MAXSIZE

[End of If structure]

Step 3: [Insert item at front end of queue]


Set queue[front]=item
Step 4: return

Deletion from Double Ended Queue


Algorithm

DEQUEUE_FRONT(queue,front,rear,MAXSIZE)

Step1: [Is queue already empty?]


If front = -1 then
write “Queue underflow error”
return -1
[End of if structure]
Step 2: [Store front item of queue]

set delitem = queue[front]

Step 3: [Find new value of front]


If front=rear then
set front = -1 and rear = -1
else
set front = (front+1)%MAXSIZE
[End of If structure]
Step 4: return delitem

DEQUEUE_REAR(queue,front,rear,MAXSIZE)

Step1: [Is queue already empty?]


If front = -1 then
write “Queue underflow error”
return -1
[End of if structure]
Step 2: [Store rear item of queue]

set delitem = queue[rear]

Step 3: [Find new value of rear]


If front=rear then
set front = -1 and rear = -1
else
set rear = (rear+MAXSIZE-1)%MAXSIZE
[End of If structure]
Step 4: return delitem
/*A PROGRAM TO PERFORM OPERATIONS ON DOUBLE ENDED QUEUE*/

#include<stdio.h>

#include<stdlib.h>

#define MAXSIZE 5

int queue[MAXSIZE];

int front=-1,rear=-1;

int isFULL(void)

if((front==0&&rear==MAXSIZE-1)||(front==rear+1))

return 1;

return 0;

int isEMPTY(void)

if(front==-1)

return 1;

return 0;

void ENQUEUE_REAR(int item)

if(isFULL())

printf("\nQueue Overflow error.");


else

if(isEMPTY())

front=rear=0;

else

rear=(rear+1)%MAXSIZE;

queue[rear]=item;

printf("\n%d is inserted in Queue",item);

void ENQUEUE_FRONT(int item)

if(isFULL())

printf("\nQueue Overflow error.");

else

if(isEMPTY())

front=rear=0;

else

front=(front+MAXSIZE-1)%MAXSIZE;

queue[front]=item;

printf("\n%d is inserted in Queue",item);

}
}

int DEQUEUE_FRONT(void)

int delitem;

if(isEMPTY())

printf("\nQueue underflow error.");

return -1;

delitem=queue[front];

if(front==rear)

front=rear=-1;

else

front=(front+1)%MAXSIZE;

return delitem;

int DEQUEUE_REAR(void)

int delitem;
if(isEMPTY())

printf("\nQueue underflow error.");

return -1;

delitem=queue[rear];

if(front==rear)

front=rear=-1;

else

rear=(rear+MAXSIZE-1)%MAXSIZE;

return delitem;

int PEEK_FRONT(void)

if(isEMPTY())

printf("Queue underflow error.");

return -1;

return queue[front];
}

int PEEK_REAR(void)

if(isEMPTY())

printf("Queue underflow error.");

return -1;

return queue[rear];

void DISPLAY(void)

int i;

if(isEMPTY())

printf("\nQueue is empty.");

return;

printf("Items of Queue are\n");

for(i=front;i!=rear;i=(i+1)%MAXSIZE)

printf("%d\t",queue[i]);
printf("%d",queue[i]);

int main()

int item,choice;

while(1)

printf("\n\t\t\t_ _ _ _ OPERATIONS ON DEQUE _ _ _ _\n");

printf("\n\n\t\t\t1. Insertion at Rear end");

printf("\n\n\t\t\t2. Insertion at Front end");

printf("\n\n\t\t\t3. Deletion from Front end");

printf("\n\n\t\t\t4. Deletion from Rear end");

printf("\n\n\t\t\t5. PEEK(Front)");

printf("\n\n\t\t\t6. PEEK(Rear)");

printf("\n\n\t\t\t7. DISPLAY");

printf("\n\n\t\t\t8. EXIT");

printf("\n\nEnter your choice:");

scanf("%d",&choice);

switch(choice)

case 1:

printf("\nEnter the item:");


scanf("%d",&item);

ENQUEUE_REAR(item);

break;

case 2:

printf("\nEnter the item:");

scanf("%d",&item);

ENQUEUE_FRONT(item);

break;

case 3:

item=DEQUEUE_FRONT();

if(item!=-1)

printf("\n%d is deleted from Queue",item);

break;

case 4:

item=DEQUEUE_REAR();

if(item!=-1)

printf("\n%d is deleted from Queue",item);

break;

case 5:

item=PEEK_FRONT();

if(item!=-1)

printf("\nItem at front of Queue = %d",item);


break;

case 6:

item=PEEK_REAR();

if(item!=-1)

printf("\nItem at rear of Queue = %d",item);

break;

case 7:

DISPLAY();

break;

case 8:

printf("\n\t\t***GOOD BYE***");

exit(0);

default:

printf("\nWrong choice.");

return 0;

}
Priority Queue

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.

In real word example when a VIP comes for a service, he will get the service before
the other people standing in a Queue because he has the priority over other people.

Types of Priority Queue


There are two types of priority queue-

Ascending order priority queue

In an ascending priority queue, elements can be inserted in any order. But, while
deleting an element from the queue, only the smallest element is removed first.

Descending order priority queue

In descending priority queue, also elements can be inserted in any order. But, while
deleting an element from the queue, only the largest element is deleted first.

Insertion in Priority Queue


Algorithm

ENQUEUE(queue,front,rear,item,MAXSIZE)

Step1: [Is queue already full?]


If rear=MAXSIZE-1 then
write “Queue overflow error”
return
[End of if structure]
Step 2: [Finding value of rear and insert item at correct position]
If front = -1 then

set front = 0 and rear = 0

queue[rear] = item

else

set j = rear

repeat while j>=front and item<queue[j]

set queue[j+1] = queue[j]

set j = j-1

set queue[j+1]=item

set rear = rear+1

[End of If structure]

Step 3: return

Deletion from Priority Queue


Algorithm

DEQUEUE(queue,front,rear)

Step1: [Is queue already empty?]


If front = -1 then
write “Queue underflow error”
return
[End of if structure]
Step 2: [Store front item of queue]

set delitem = queue[front]

Step 3: [Find new value of front]


If front=rear then
set front = -1 and rear = -1
else
set front = front+1
[End of If structure]
Step 4: return delitem
/*A PROGRAM TO PERFORM OPERATIONS ON PRIORITY QUEUE*/

#include<stdio.h>

#include<stdlib.h>

#define MAXSIZE 5

int queue[MAXSIZE];

int front=-1,rear=-1;

int isFULL(void)

if(rear==MAXSIZE-1)

return 1;

return 0;

int isEMPTY(void)

if(front==-1)

return 1;

return 0;

void ENQUEUE(int item)

int j;
if(isFULL())

printf("\nQueue Overflow error.");

else

if(isEMPTY())

front=rear=0;

queue[rear]=item;

else

j=rear;

while(j>=front&&item<queue[j])

queue[j+1]=queue[j];

j=j-1;

queue[j+1]=item;

rear=rear+1;

printf("\n%d is inserted in Queue",item);

}
int DEQUEUE(void)

int delitem;

if(isEMPTY())

printf("Queue underflow error.");

return -1;

delitem=queue[front];

if(front==rear)

front=rear=-1;

else

front=front+1;

return delitem;

int PEEK(void)

if(isEMPTY())

printf("Queue underflow error.");

return -1;
}

return queue[front];

void DISPLAY(void)

int i;

if(isEMPTY())

printf("Queue is empty.");

else

printf("Items of Queue are\n");

for(i=front;i<=rear;i++)

printf("%d\t",queue[i]);

int main()

int item,choice;

while(1)

{
printf("\n\t\t\t_ _ _ _ OPERATIONS ON PRIORITY QUEUE _ _ _ _\n");

printf("\n\n\t\t\t1. INSERTION");

printf("\n\n\t\t\t2. DELETION");

printf("\n\n\t\t\t3. PEEK");

printf("\n\n\t\t\t4. DISPLAY");

printf("\n\n\t\t\t5. EXIT");

printf("\n\nEnter your choice:");

scanf("%d",&choice);

switch(choice)

case 1:

printf("\nEnter the item:");

scanf("%d",&item);

ENQUEUE(item);

break;

case 2:

item=DEQUEUE();

if(item!=-1)

printf("\n%d is deleted from Queue",item);

break;

case 3:

item=PEEK();

if(item!=-1)
printf("\nItem at Front of Queue = %d",item);

break;

case 4:

DISPLAY();

break;

case 5:

printf("\n\n\t\t\t_ _ _ Good Bye _ _ _");

exit(0);

default:

printf("\nWrong choice.");

return 0;

You might also like