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

Queue

Uploaded by

sahilkatiyar860
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Queue

Uploaded by

sahilkatiyar860
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

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.
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.

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.

Characteristics of Queue:
 Queue can handle multiple data.
 We can access both ends.
 They are fast and flexible.

Queue Operations



The queue is an abstract data type which is defined by following
structure and operations.
Queue is structured as an ordered collection of items which are
added at one end called rear end, and other end called front end.
The queue operations are as follows:
1. create
2. enqueue
3. dequeue
4. isEmpty
5. isFull
6. size
1) create : Creates and initializes new queue that is empty, it does
not require any parameter and returns an empty queue.
2) enqueue: Add a new element to the rear of queue . It requires
the element to be added and returns nothing.
3) dequeue: removes the elements from the front of queue. It
does not require any parameters and returns the deleted item.
4) isEmpty: Check the whether the queue is empty or not. It does
not require any parameter and returns a Boolean value.
Given N integers, the task is to insert those elements in the queue.
Also, given M integers, the task is to find the frequency of each
number in the Queue.
Examples:
Input:
N=8
1 2 3 4 5 2 3 1 (N integers)
M=5
1 3 2 9 10 (M integers)
Output:
2 2 2 -1 -1
Explanation:
After inserting 1, 2, 3, 4, 5, 2, 3, 1 into the queue, frequency of 1 is
2, 3 is 2, 2 is 2, 9 is -1 and 10 is -1 (since, 9 and 10 is not there in
the queue).
Input:
N=5
5 2 2 4 5 (N integers)
M=5
2 3 4 5 1 (M integers)
Output:
2 -1 1 2 -1
Approach: The given problem can be solved by using a
simple queue. The idea is to insert N integers one by one into
the queue and then check the frequency of M integers. Separate
functions will be created to perform both operations. Follow the
steps below to solve the problem.
 Create a queue and push all given integers into the queue.
 After pushing all the elements into the queue create
another function for counting the frequency of
given M elements one by one.
 Take a variable cntFrequency to keep track of frequency
of current element.
 Pop all the elements from queue till its size and each time
check if current element in queue is equal to element for
which we are checking for frequency.
– if both are equal, increment cntFrequency by 1
– else, do nothing.
 Push the current element back to the queue so that for next
case queue will not get disturbed.
 Print the frequency of each element found.
Time Complexity: O(N*M)
Auxiliary Space: O(N)
Difference between Stack and Queue Data Structures are as
follows:

Stacks Queues

A stack is a data structure that stores A queue is a data structure that stores a
a collection of elements, with collection of elements, with operations to
operations to push (add) and pop enqueue (add) elements at the back of the
(remove) elements from the top of queue, and dequeue (remove) elements from
the stack. the front of the queue.

Stacks are based on the LIFO


Queues are based on the FIFO principle, i.e.,
principle, i.e., the element inserted
the element inserted at the first, is the first
at the last, is the first element to
element to come out of the list.
come out of the list.

Stacks are often used for tasks that


Queues are often used for tasks that involve
require backtracking, such as
processing elements in a specific order, such
parsing expressions or
as handling requests or scheduling tasks.
implementing undo functionality.

Insertion and deletion in queues takes place


Insertion and deletion in stacks from the opposite ends of the list. The
takes place only from one end of the insertion takes place at the rear of the list and
list called the top. the deletion takes place from the front of the
list.

Insert operation is called push


Insert operation is called enqueue operation.
operation.

Stacks are implemented using an Queues are implemented using an array or


array or linked list data structure. linked list data structure.
Stacks Queues

Delete operation is called pop


Delete operation is called dequeue operation.
operation.

In queues we maintain two pointers to access


In stacks we maintain only one
the list. The front pointer always points to the
pointer to access the list, called the
first element inserted in the list and is still
top, which always points to the last
present, and the rear pointer always points to
element present in the list.
the last inserted element.

Stack is used in solving problems Queue is used in solving problems having


works on recursion. sequential processing.

Stacks are often used for recursive Queues are often used in multithreaded
algorithms or for maintaining a applications, where tasks are added to a queue
history of function calls. and executed by a pool of worker threads.

Queue is of three types – 1. Circular Queue 2.


Stack does not have any types.
Priority queue 3. double-ended queue.

Can be considered as a vertical Can be considered as a horizontal collection


collection visual. visual.

Examples of queue-based algorithms include


Examples of stack-based languages
Breadth-First Search (BFS) and printing a
include PostScript and Forth.
binary tree level-by-level.

Applications of stack:
 Some CPUs have their entire assembly language based on
the concept of performing operations on registers that are
stored in a stack.
 Stack structure is used in the C++ run-time system.
Applications of queue:
 Queue data structure is implemented in the hardware
microinstructions inside a CPU.
 Queue structure is used in most operating systems.

Applications, Advantages and


Disadvantages of Queue



A Queue is a linear data structure. This data structure follows a
particular order in which the operations are performed. The order
is First In First Out (FIFO). It means that the element that is inserted
first in the queue will come out first and the element that is inserted
last will come out last. It is an ordered list in which insertion of an
element is done from one end which is known as the rear end and
deletion of an element is done from the other which is known as the
front end. Similar to stacks, multiple operations can be performed on
the queue. When an element is inserted in a queue, then the
operation is known as Enqueue and when an element is deleted
from the queue, then the operation is known as Dequeue. It is
important to know that we cannot insert an element if the size of the
queue is full and cannot delete an element when the queue itself is
empty. If we try to insert an element even after the queue is full,
then such a condition is known as overflow whereas, if we try to
delete an element even after the queue is empty then such a
condition is known as underflow.
Primary Queue Operations:
 void enqueue(int Element): When this operation is
performed, an element is inserted in the queue at the end
i.e. at the rear end. (Where T is Generic i.e we can define
Queue of any type of data structure.) This operation
take constant time i.e O(1).
 int dequeue(): When this operation is performed, an
element is removed from the front end and is returned. This
operation take constant time i.e O(1).
Auxiliary Queue Operations:
 int front(): This operation will return the element at the
front without removing it and it take O(1) time.
 int rear(): This operation will return the element at the rear
without removing it, Its Time Complexity is O(1).
 int isEmpty(): This operation indicates whether the queue
is empty or not. This Operation also done in O(1).
 int size(): This operation will return the size of the queue
i.e. the total number of elements present in the queue and
it’s time complexity is O(n).
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.
 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.
 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.
 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 the 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.
Implementation of Queue:
 Sequential allocation: A queue can be implemented using
an array. It can organize a limited number of elements.
 Linked list allocation: A queue can be implemented
using a linked list. It can organize an unlimited number of
elements.
Applications of Queue:
 Multi programming: Multi programming means when
multiple programs are running in the main memory. It is
essential to organize these multiple programs and these
multiple programs are organized as queues.
 Network: In a network, a queue is used in devices such as
a router or a switch. another application of a queue is a mail
queue which is a directory that stores data and controls files
for mail messages.
 Job Scheduling: The computer has a task to execute a
particular number of jobs that are scheduled to be executed
one after another. These jobs are assigned to the processor
one by one which is organized using a queue.
 Shared resources: Queues are used as waiting lists for a
single shared resource.
Real-time application of Queue:
 ATM Booth Line
 Ticket Counter Line
 Key press sequence on the keyboard
 CPU task scheduling
 Waiting time of each customer at call centers.
Advantages of Queue:
 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.
 Queues are useful when a particular service is used by
multiple consumers.
 Queues are fast in speed for data inter-process
communication.
 Queues can be used in the implementation of other data
structures.
Disadvantages of Queue:
 The operations such as insertion and deletion of elements
from the middle are time consuming.
 Limited Space.
 In a classical queue, a new element can only be inserted
when the existing elements are deleted from the queue.
 Searching an element takes O(N) time.
 Maximum size of a queue must be defined prior.

You might also like