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