You are on page 1of 33

QUEUE

Queue
• Like Stack, Queue is a linear structure which follows a particular order in which
the operations are performed. The order is First In First Out (FIFO).  A good
example of queue is any queue of consumers for a resource where the consumer
that came first is served first.
•  Unlike stacks, a queue is open at both its ends. One end is always
used to insert data (enqueue) and the other is used to remove data
(dequeue). So it uses two pointers front and rear to delete and insert
elements respectively.
• The difference between stacks and queues is in removing. In a stack we remove
the item the most recently added; in a queue, we remove the item the least
recently added.
Elements of queue:-
• Front: -
• This end is used for deleting an element from a queue.
Initially front end is set to -1. Front end is incremented by
one when a new element has to be deleted from queue.
Rear: -
This end is used for inserting an element in a queue.
Initially rear end is set to -1. rear end is incremented by one
when a new element has to be inserted in queue.
Operations on Queue:
Mainly the following 2 basic operations are performed on queue:
• Enqueue: Adds an item to the queue. If the queue is full, then it is said to be an Overflow
condition.
• Dequeue: Removes an item from the queue. The items are popped in the same order in
which they are pushed. If the queue is empty, then it is said to be an Underflow condition.
Front=-1 Queue is empty Front=1 Delete

1 5 B C
0 1 2 3 4 0 1 2 3 4
Rear=-1 Rear=2
Front=0 Insert A Front=2 Delete

2 A 6 C
0 1 2 3 4 0 1 2 3 4
Rear=0 Rear=2
Front=0 Insert B Front=3 Delete

3 A B 7

0 1 2 3 4 0 1 2 3 4
Rear=1 Rear=2 Queue is empty
Front=0 Insert C
Entry point is called Rear &
4 A B C
Exit point is called Front
0 1 2 3 4
Rear=2 5
Enqueue Operation
• As we know, Queues maintain two data pointers, front and rear. Therefore,
its operations are comparatively difficult to implement than that of stacks.
• The following steps should be taken to enqueue (insert) data into a queue −
• Step 1 − Check if the queue is full.
• Step 2 − If the queue is full, produce overflow error and exit.
• Step 3 − If the queue is not full, increment rear pointer to point the next
empty space.
• Step 4 − Add data element to the queue location, where the rear is pointing.
• Step 5 − return success.
Enqueue 5,F 6,R

void enqueue()
{ int x;
if(rear==max-1) front=-1, rear=-1
printf("Queue is overflow\n");
else
{if(front==-1) front++; //only if we are adding first element
printf("Enter element to be insert:");
scanf("%d",&x);
Rear++;
q[rear]=x;
}}
Dequeue Operation
• Accessing data from the queue is a process of two tasks − access the data where front is pointing
and remove the data after access. The following steps are taken to perform dequeue operation −
• Step 1 − Check if the queue is empty.
• Step 2 − If the queue is empty, produce underflow error and exit.
• Step 3 − If the queue is not empty, access the data where front is pointing.
• Step 4 − Increment front pointer to point to the next available data element.
• Step 5 − Return success.
Dequeue
void dequeue()
{
if((front==-1)||(front==rear+1))
Cout<<“underflow”;
else
{
cout<<q[front];
front++;
}
}Note*://front will be –1 only at beginning of queue implementation.
To display elements of queue
void display()
{
int i;
if(front==-1&&rear==-1)
{
printf("Queue is empty\n");
}
else{
for(i=front;i<=rear;i++)
printf("\t%d",q[i]);
printf("\n");
}}
Disadvantages of linear queue

• On deletion of an element from existing queue, front pointer is shifted


to next position.
• This results into freeing of space but due to rear pointer it cant be
utilized.
• By doing so memory space which was occupied by deleted element is
wasted and hence inefficient memory utilization is occur.
Overcome disadvantage of linear queue:

• To overcome disadvantage of linear queue, circular queue is use.


• We can solve this problem by joining the front and rear end of a queue to make
the queue as a circular queue .
• Circular queue is a linear data structure. It follows FIFO principle.
• In circular queue the last node is connected back to the first node to make a
circle.
• Circular queue avoids the wastage of space in a regular queue implementation
using arrays.

• As you can see in the above image, after a bit of enqueueing and dequeuing, the
size of the queue has been reduced.
• The indexes 0 and 1 can only be used after the queue is reset when all the
elements have been dequeued.
Overcome disadvantage of linear queue:

• It is also called as “Ring buffer”.


• Items can inserted and deleted from a queue in
O(1) time.
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’.
• In a normal Queue, we can insert elements until queue becomes full.
But once queue becomes full, we can not insert the next element
even if there is a space in front of queue.
Operations on Circular Queue:
• enQueue(value) This function is used to insert an element into the circular queue. In
a circular queue, the new element is always inserted at Rear position. Steps:
1. Check whether queue is Full – Check
((rear == SIZE-1 && front == 0) || (rear == front-1)).
2. If it is full then display Queue is full.
If queue is not full then,
check if (rear == SIZE – 1 && front != 0)
if it is true then set rear=0 and insert element.
enqueue(int queue[], int item)
{
if ((front == 0 && rear == size - 1) ||
(front == rear + 1))
//this is underflow condition in simple
{ queue(after deleting last element) which is
printf(“overflow"); eliminated by making f=r=-1 every time last
element will be deleted.
}
else if (rear == - 1)
//adding first element then change
{ front also.
rear++;
front++;
}
else Else
if (rear == size - 1 && front != 0)
Rear=(rear+1)%size
{
rear = 0; Queue[rear]=item;
}
else
{
rear++;
}
queue[rear] = item;
}
• deQueue() This function is used to delete an element from the
circular queue. In a circular queue, the element is always deleted
from front position.
• Steps:
1. Check whether queue is Empty means check (front==-1).
2. If it is empty then display Queue is empty. If queue is not empty then step 3
3. Check if (front==rear)
if it is true then set front=rear= -1
else check if (front==size-1),
if it is true then set front=0 and return the element.
deleteq(int queue[])
else
{
if (front == - 1)
{
{ printf("\n %d deleted",
printf("Queue is empty "); queue[front]);
} front++;
else if (front == rear) }
{
printf("\n %d deleted", queue[front]);
front = - 1;
rear = - 1;
}
Display circular queue
display(int queue[]) for (i = 0; i <= rear; i++)
{ printf("%d ", queue[i]);
int i;
}
printf("\n");
if (front > rear) else
{ {
for (i = front; i < size; i++) for (i = front; i <= rear; i++)
{
printf("%d ", queue[i]);
printf("%d ", queue[i]);
}
}
}
PRIORITY QUEUE
• Priority Queue is an abstract data type, which is similar to a queue,
however, in the priority queue, every element has some priority. The
priority of the elements in a priority queue determines the order in
which elements are removed from the priority queue. Therefore all
the elements are either arranged in an ascending or descending order.
So, a priority Queue is an extension of the queue with the following
properties:

• Every item has a priority associated with it.


• An element with high priority is dequeued before an element with low
priority.
• If two elements have the same priority, they are served according to their
order in the queue.
• In Priority queue items are ordered by key value so that item with the
lowest value of key is at front and item with the highest value of key is
at rear or vice versa. So we're assigned priority to item based on its
key value. Lower the value, higher the priority. Following are the
principal methods of a Priority Queue.
PRIORITY QUEUE
• Advantages:-
• Preferences to the higher priority process are
added at the beginning.
• Keep the list sorted in increasing order.
Applications of Queue

• Queue, as the name suggests is used whenever we need to have any group of
objects in an order in which the first one coming in, also gets out first while
the others wait for there turn, like in the following scenarios :
• 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.

2 4 5 6 7 9,R
Applications..
An example where priority queue are used is in operating systems.
• The operating system has to handle a large number of jobs.
• These jobs have to be properly scheduled.
• The operating system assigns priorities to each type of job.
• The jobs are placed in a queue and the job with the highest priority will be
executed first.
Basic Operations

• insert / enqueuer/add − add an item to the rear of the queue.


• remove / dequeuer/poll − remove an item from the front of the
queue.
• Priority Queue Representation
Implementation of Priority Queue

• Priority queue can be implemented using an array, a linked list, a heap


data structure, or a binary search tree. Among these data structures,
heap data structure provides an efficient implementation of priority
queues.
process
• We have a priority queue that contains the following values:
• 1, 3, 4, 8, 14, 22

• All the values are arranged in ascending order. Now, we will observe how the priority queue
will look after performing the following operations:
• dequeue(): This function will remove the highest priority element from the priority queue. In
the above priority queue, the '1' element has the highest priority, so it will be removed from
the priority queue.
• enqueue(2): This function will insert '2' element in a priority queue. As 2 is the smallest
element among all the numbers so it will obtain the highest priority.
• dequeue(): It will remove '2' element from the priority queue as it has the highest priority
queue.
• enqueue(5): It will insert 5 element after 4 as 5 is larger than 4 and lesser than 8, so it will
obtain the third highest priority in a priority queue.

You might also like