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

Chapter 5 Queue

Uploaded by

hammadmomin2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Chapter 5 Queue

Uploaded by

hammadmomin2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Chapter 5 Queue

Queue
1. A queue can be defined as an ordered list which enables insert operations
to be performed at one end called REAR and delete operations to be
performed at another end called FRONT.

2. Queue is referred to be as First In First Out list.

3. For example, people waiting in line for a rail ticket form a queue.
Applications of Queue

1. Queues are widely used as waiting lists for a single shared resource like
printer, disk, CPU.
2. Queues are used in asynchronous transfer of data (where data is not
being transferred at the same rate between two processes) for eg.
pipes, file IO, sockets.
3. Queues are used as buffers in most of the applications like MP3 media
player, CD player, etc.
4. Queue are used to maintain the play list in media players in order to
add and remove the songs from the play-list.
5. Queues are used in operating systems for handling interrupts.

Types of Queue
There are four different types of queue that are listed as follows -
o Simple Queue or Linear Queue
o Circular Queue
o Priority Queue
o Double Ended Queue (or Deque)

Simple Queue or Linear Queue


In Linear Queue, an insertion takes place from one end while the deletion
occurs from another end. The end at which the insertion takes place is known
as the rear end, and the end at which the deletion takes place is known as
front end. It strictly follows the FIFO rule.
The major drawback of using a linear Queue is that insertion is done only from the
rear end. If the first three elements are deleted from the Queue, we cannot insert
more elements even though the space is available in a Linear Queue. In this case,
the linear Queue shows the overflow condition as the rear is pointing to the last
element of the Queue.

Circular Queue
In Circular Queue, all the nodes are represented as circular. It is similar to the
linear Queue except that the last element of the queue is connected to the
first element. It is also known as Ring Buffer, as all the ends are connected to
another end. The representation of circular queue is shown in the below
image -

The drawback that occurs in a linear queue is overcome by using the circular
queue. If the empty space is available in a circular queue, the new element can be
added in an empty space by simply incrementing the value of rear. The main
advantage of using the circular queue is better memory utilization.

Priority Queue

It is a special type of queue in which the elements are arranged based on the
priority. It is a special type of queue data structure in which every element has a
associated with it. Suppose priority some elements occur with the same priority,
they will be arranged according to the FIFO principle. The representation of priority
queue is shown in the below image –
insertion in priority queue takes place based on the arrival, while deletion in the
priority queue occurs based on the priority. Priority queue is mainly used to
implement the CPU scheduling algorithms.

Deque (or, Double Ended 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. It means that we can
insert and delete elements from both front and rear ends of the queue. Deque
can be used as a palindrome checker means that if we read the string from
both ends, then the string would be the same.
Deque can be used both as stack and queue as it allows the insertion and
deletion operations on both ends. Deque can be considered as stack because
stack follows the LIFO (Last In First Out) principle in which insertion and
deletion both can be performed only from one end. And in deque, it is
possible to perform both insertion and deletion from one end, and Deque
does not follow the FIFO principle.

Operations performed on queue


The fundamental operations that can be performed on queue are listed as
follows -
o Enqueue: The Enqueue operation is used to insert the element at the rear end
of the queue. It returns void.
o Dequeue: It performs the deletion from the front-end of the queue. It also
returns the element which has been removed from the front-end. It returns
an integer value.
o Peek: This is the third operation that returns the element, which is pointed by
the front pointer in the queue but does not delete it.
o Queue overflow (isfull): It shows the overflow condition when the queue is
completely full.
o Queue underflow (isempty): It shows the underflow condition when the
Queue is empty, i.e., no elements are in the Queue.

Ways to implement the queue


There are two ways of implementing the Queue:
o Implementation using array: The sequential allocation in a Queue can be
implemented using an array.
o Implementation using Linked list: The linked list allocation in a Queue can be
implemented using a linked list.

Algorithm to insert any element in a queue


Step 1: IF REAR = MAX - 1

Write OVERFLOW

Go to step

[END OF IF]

Step 2: IF FRONT = -1 and REAR = -1

SET FRONT = REAR = 0

ELSE

SET REAR = REAR + 1


[END OF IF]

Step 3: Set QUEUE[REAR] = NUM

Step 4: EXIT

Algorithm to delete an element from the queue


If, the value of front is -1 or value of front is greater than rear , write an underflow
message and exit.

OthStep 2: EXIT
int delete (int queue[], int max,erwise, keep increasing the value of front and return the
item stored at the front end of the queue at each time.
Algorithm
Step 1: IF FRONT = -1 or FRONT > REAR
Write UNDERFLOW
ELSE
SET VAL = QUEUE[FRONT]
Step 2 :I if front=rear
front=-1
rear =-1
else
SET FRONT = FRONT + 1
[END OF IF]
Step 3: Exit

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy