Implementation of Queue Using Array

Video Tutorial
FREE
Queue Implementation Using LinkedList thumbnail
This video belongs to
Java DSA Course - Master the Fundamentals and Beyond
12 modules
Certificate
Topics Covered

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.

  1. If n-1==REAR then this means the queue is already full.
  2. But if REAR<n means that we can store an element in an array.
  3. 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.

  1. If front == - 1 or front > rear then no element is available to delete.
  2. Else delete FRONT index element
  3. If REAR==FRONT then we set -1 to both FRONT AND REAR
  4. Else we increment FRONT.

Front

Returns the FRONT value of queue.

  1. First of all we need to check that queue is not empty.
  2. 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
  3. Otherwise, we will return the FRONT index value.

Display

It will traverse the queue and print all the elements of the queue.

  1. Firstly check whether the queue is not empty.
  2. If empty we display that the queue is empty we simply return from the function and not execute further inside the function.
  3. 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

empty array creation of size 5

enqueue(2) Refer below image to show the enqueue operation enqueue operation

front()

enqueue(9) Refer below image to show the enqueue operation Array for the implementation of queue

display()

enqueue(7) Refer below image to show the enqueue operation Array Enqueue Operation

dequeue() Refer below image to show the dequeue operation Dequeue Operation

display()

enqueue(20) enqueue(30) Refer below image to show the enqueue operation Queue Implementation

display()

dequeue() Refer below image to show the dequeue operation Array dequeue operation

dequeue() Refer below image to show the dequeue operation Final 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 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.