Queue
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
• 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:
• 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
• 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.