Unit II - Topic 3 - Queue (Exp.3.a & Exp .3.b.)
Unit II - Topic 3 - Queue (Exp.3.a & Exp .3.b.)
Characteristics of Queue:
Queue can handle multiple data.
We can access both ends.
They are fast and flexible.
Queue Representation:
Like stacks, Queues can also be represented in an array: In this representation, the Queue is implemented
using the array. Variables used in this case are
Queue: the name of the array storing queue elements.
Front: the index where the first element is stored in the array representing the queue.
Rear: the index where the last element is stored in an array representing the queue.
2
Unit II – Topic 3 – Queue
Similar to Stack, Queue is a linear data structure that follows a particular order in which the operations are
performed for storing data. The order is First In First Out (FIFO).
One can imagine a queue as a line of people waiting to receive something in sequential order which starts
from the beginning of the line. It is an ordered list in which insertions are done at one end which is known as
the rear and deletions are done from the other end known as the front. A good example of a queue is any
queue of consumers for a resource where the consumer that came first is served first.
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.
3
Unit II – Topic 3 – Queue
Types of Queues:
Simple Queue: Simple queue also known as a linear queue is the most basic version of a queue. Here,
insertion of an element i.e. the Enqueue operation takes place at the rear end and removal of an element
i.e. the Dequeue operation takes place at the front end. Here problem is that if we pop some item from
front and then rear reach to the capacity of the queue and although there are empty spaces before front
4
Unit II – Topic 3 – Queue
means the queue is not full but as per condition in isFull() function, it will show that the queue is full
then. To solve this problem we use circular queue.
Circular Queue: In a circular queue, the element of the queue act as a circular ring. The working of a
circular queue is similar to the linear queue except for the fact that the last element is connected to the
first element. Its advantage is that the memory is utilized in a better way. This is because if there is an
empty space i.e. if no element is present at a certain position in the queue, then an element can be easily
added at that position using modulo capacity(%n).
Priority Queue: This queue is a special type of queue. Its specialty is that it arranges the elements in
a queue based on some priority. The priority can be something where the element with the highest
value has the priority so it creates a queue with decreasing order of values. The priority can also be
such that the element with the lowest value gets the highest priority so in turn it creates a queue with
increasing order of values. In pre-define priority queue, C++ gives priority to highest value whereas
Java gives priority to lowest value.
Dequeue: Dequeue is also known as Double Ended Queue. As the name suggests double ended, it
means that an element can be inserted or removed from both ends of the queue, unlike the other
queues in which it can be done only from one end. Because of this property, it may not obey the First
In First Out property.
Some common applications of Queue data structure :
1. Task Scheduling: Queues can be used to schedule tasks based on priority or the order in which they
were received.
2. Resource Allocation: Queues can be used to manage and allocate resources, such as printers or CPU
processing time.
3. Batch Processing: Queues can be used to handle batch processing jobs, such as data analysis or image
rendering.
5
Unit II – Topic 3 – Queue
4. Message Buffering: Queues can be used to buffer messages in communication systems, such as
message queues in messaging systems or buffers in computer networks.
5. Event Handling: Queues can be used to handle events in event-driven systems, such as GUI
applications or simulation systems.
6. Traffic Management: Queues can be used to manage traffic flow in transportation systems, such as
airport control systems or road networks.
7. Operating systems: Operating systems often use queues to manage processes and resources. For
example, a process scheduler might use a queue to manage the order in which processes are executed.
8. Network protocols: Network protocols like TCP and UDP use queues to manage packets that are
transmitted over the network. Queues can help to ensure that packets are delivered in the correct order
and at the appropriate rate.
9. Printer queues :In printing systems, queues are used to manage the order in which print jobs are
processed. Jobs are added to the queue as they are submitted, and the printer processes them in the order
they were received.
10. Web servers: Web servers use queues to manage incoming requests from clients. Requests are added
to the queue as they are received, and they are processed by the server in the order they were received.
11. Breadth-first search algorithm: The breadth-first search algorithm uses a queue to explore nodes in
a graph level-by-level. The algorithm starts at a given node, adds its neighbors to the queue, and then
processes each neighbor in turn.
6
Unit II – Topic 3 – Queue
problems, the front may reach the end of the array. The solution to this problem is to increase front and
rear in circular manner.
Steps for enqueue:
1. Check the queue is full or not
2. If full, print overflow and exit
3. If queue is not full, increment tail and add the element
Steps for dequeue:
1. Check queue is empty or not
2. if empty, print underflow and exit
3. if not empty, print element at the head and increment head
7
Unit II – Topic 3 – Queue
8
Unit II – Topic 3 – Queue