0% found this document useful (0 votes)
32 views9 pages

Unit II - Topic 3 - Queue (Exp.3.a & Exp .3.b.)

A Queue is a linear data structure that operates on a First In First Out (FIFO) basis, where elements are added at one end (rear) and removed from the other end (front). It can be implemented using arrays or linked lists, with various types such as simple queues, circular queues, priority queues, and double-ended queues (dequeues). Queues have numerous applications including task scheduling, resource allocation, and managing processes in operating systems.

Uploaded by

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

Unit II - Topic 3 - Queue (Exp.3.a & Exp .3.b.)

A Queue is a linear data structure that operates on a First In First Out (FIFO) basis, where elements are added at one end (rear) and removed from the other end (front). It can be implemented using arrays or linked lists, with various types such as simple queues, circular queues, priority queues, and double-ended queues (dequeues). Queues have numerous applications including task scheduling, resource allocation, and managing processes in operating systems.

Uploaded by

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

Unit II – Topic 3 – Queue

What is Queue Data Structure?


A Queue is defined as a linear data structure that is open at both ends and the operations are performed in
First In First Out (FIFO) order.
We define a queue to be a list in which all additions to the list are made at one end, and all deletions from the
list are made at the other end. The element which is first pushed into the order, the operation is first
performed on that.

FIFO Principle of Queue:


 A Queue is like a line waiting to purchase tickets, where the first person in line is the first person served.
(i.e. First come first serve).
 Position of the entry in a queue ready to be served, that is, the first entry that will be removed from the
queue, is called the front of the queue(sometimes, head of the queue), similarly, the position of the last
entry in the queue, that is, the one most recently added, is called the rear (or the tail) of the queue. See
the below figure.
1
Unit II – Topic 3 – Queue

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

Introduction and Array Implementation of 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

Basic Operations on Queue:


 enqueue(): Inserts an element at the end of the queue i.e. at the rear end.
 dequeue(): This operation removes and returns an element that is at the front end of the queue.
 front(): This operation returns the element at the front end without removing it.
 rear(): This operation returns the element at the rear end without removing it.
 isEmpty(): This operation indicates whether the queue is empty or not.
 isFull(): This operation indicates whether the queue is full or not.
 size(): This operation returns the size of the queue i.e. the total number of elements it contains.

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.

Array implementation Of Queue:


For implementing queue, we need to keep track of two indices, front and rear. We enqueue an item at the
rear and dequeue an item from the front. If we simply increment front and rear indices, then there may be

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

Advantages of Array Implementation:


 Easy to implement.
 A large amount of data can be managed efficiently with ease.
 Operations such as insertion and deletion can be performed with ease as it follows the first in first out
rule.
Disadvantages of Array Implementation:
 Static Data Structure, fixed size.
 If the queue has a large number of enqueue and dequeue operations, at some point (in case of linear
increment of front and rear indexes) we may not be able to insert elements in the queue even if the
queue is empty (this problem is avoided by using circular queue).
 Maximum size of a queue must be defined prior.

7
Unit II – Topic 3 – Queue

Queue – Linked List Implementation


we maintain two pointers, front, and rear. The front points to the first item of the queue and rear points to
the last item.
 enQueue(): This operation adds a new node after the rear and moves the rear to the next node.
 deQueue(): This operation removes the front node and moves the front to the next node.

Follow the below steps to solve the problem:


 Create a class QNode with data members integer data and QNode* next
 A parameterized constructor that takes an integer x value as a parameter and sets data equal to
x and next as NULL
 Create a class Queue with data members QNode front and rear
 Enqueue Operation with parameter x:
 Initialize QNode* temp with data = x
 If the rear is set to NULL then set the front and rear to temp and return(Base Case)
 Else set rear next to temp and then move rear to temp
 Dequeue Operation:
 If the front is set to NULL return(Base Case)
 Initialize QNode temp with front and set front to its next
 If the front is equal to NULL then set the rear to NULL
 Delete temp from the memory

Program to implement queue using linked list (Exp.3.b.)

8
Unit II – Topic 3 – Queue

You might also like