Implementation of Queue Using Array
Problem Statement
Write a program that implements the operations of queue using an array
Implementation of Queue using Array Operations
The queue is the linear data structure that works on the principle of FIFO( First In First Out). In queue insertion and deletion of elements takes place at two different ends. The addition of an element happens at an end known as REAR and deletion happens at the FRONT end. Implementation of queue using array starts with the creation of an array of size n. And initialize two variables FRONT and REAR with-1which means currently queue is empty. The REAR value represents the index up to which value is stored in the queue and the FRONT value represents the index of the first element to be dequeued
Enqueue
Insert an element from the rear end into the queue. Element is inserted into the queue after checking the overflow condition n-1==REAR to check whether the queue is full or not.
- If n-1==REAR then this means the queue is already full.
- But if REAR<n means that we can store an element in an array.
- So increment the REAR value by 1 and then insert an element at the REAR index.
Dequeue
Deleting an element from the FRONT end of the queue. Before deleting an element we need to check the underflow conditionfront == - 1 or front > rear to check whether there is at least one element available for the deletion or not.
- If front == - 1 or front > rear then no element is available to delete.
- Else delete FRONT index element
- If REAR==FRONT then we set -1 to both FRONT AND REAR
- Else we increment FRONT.
Front
Returns the FRONT value of queue.
- First of all we need to check that queue is not empty.
- If the queue is empty then we display that the queue is empty we simply return from the function and not execute further inside the function
- Otherwise, we will return the FRONT index value.
Display
It will traverse the queue and print all the elements of the queue.
- Firstly check whether the queue is not empty.
- If empty we display that the queue is empty we simply return from the function and not execute further inside the function.
- Else we will print all elements from FRONT to REAR.
Example
Let us understand implementation of queue using array problem by an example n=5
Refer to the below image to show an empty array creation of size 5
enqueue(2) Refer below image to show the enqueue operation
front()
enqueue(9) Refer below image to show the enqueue operation
display()
enqueue(7) Refer below image to show the enqueue operation
dequeue() Refer below image to show the dequeue operation
display()
enqueue(20) enqueue(30) Refer below image to show the enqueue operation
display()
dequeue() Refer below image to show the dequeue operation
dequeue() Refer below image to show the dequeue operation
display()
Example Explanation
In the above example of implementation of queue using array problem, first of all we are creating an array named arr[] of size n which means the capacity of the queue is n and initializing FRONT and REAR as -1. Now for enqueue operation we increment REAR and insert an element at REAR arr[REAR]=item. For the dequeue operation, we delete an element at the FRONT index and increment FRONT by 1.
Input
The capacity of the queue is provided and queries are given as input to perform operations on the queue
Task
You need to perform all input operations of the queue with the help of an array.
Output
- enqueue() will print overflow if queue is already full otherwise it simply insert value * dequeue() will print underflow if queue is empty otherwise it will print deleted value. * display() will print all the elements of queue
- front() will print front element of queue if queue is not
Constraints
Implementation of a Queue using Array
Algorithm
enqueue(item)
Step 1:
Step 2:
Step 3:
Step 4:
dequeue()
Step 1:
Step 2:
Step 3:
front()
Step 1:
Step 2:
display()
Step 1:
Step 2:
C++ Implementation C++ implementation of implementation of queue using array problem
Java implementation Implementation of queue using array problem java implementation
Python implementation Implementation of queue using array problem python implementation
Output:
Complexity
Time Complexity
enqueue(),dequeue(),front() can be perfromed in constant time. So time Complexity of enqueue, dequeue() , front() are O(1) but display will print all the elements of queue. So its time complexity is O(N). Overall worst case time complexity of implementation of queue using array is O(N).
Space Complexity
As we are using an array of size n to store all the elements of the queue so space complexity using array implemenation is O(N).
Drawbacks of Queue Implementation Using Array
Memory Wastage
In Implementation of queue using array there is a wastage of memory. The available free space in the array can not be used for storing elements.
Refer below image to show memory wastage problem
- In the above scenario free space is available in the array but when we try to insert element.
- Then REAR==N-1 condition becomes true so it will print overflow and insertion will not take place.
- So in array implementation we are not able to insert new elements even when the free space is available in the array.
Declaring the Array Size
- One of the common issue with array is that it requires static memory allocation, means we need to define array size at time of declaration of array.
- Change in size of array at the run time is very expensive process.
- So it is require to declare the array size in advance. Due to this, we declare a large size so that maximum possible queue elements are stored in array. But many slots of array may remain left unused which lead to wastage of memory.
- To overcome this, if we declare a small size array then it may become difficult to store all queue elements.
Conclusion
- Queue is a data structure that works on the principle of First In First Out(FIFO).
- Enqueue operation is performed to insert the element at the rear end of the queue.
- Dequeue operation is performed to delete elements from the front end of the queue.
- Display operation traverse the queue and print all its elements.
- Front will return the front element of the queue if the queue is not empty.
- There are some drawbacks of implementation of queue using array problem like memory wastage, and declaration of array size in advance.