VOOZH about

URL: https://www.geeksforgeeks.org/dsa/array-implementation-of-queue-simple/

⇱ Queue using Array - Simple Implementation - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Queue using Array - Simple Implementation

Last Updated : 20 Sep, 2025

A queue is a linear data structure that follows the FIFO (First In, First Out) principle. The first element inserted is the first one to be removed.

Declaration of Queue Using Array:

A queue can be implemented using an array, and there are two main ways:

  1. Infinite (or Dynamically Growable) Array Queue
  2. Fixed-Size Array Queue

Infinite (or Dynamically Growable) Array Queue:

We can implement a queue using a conceptually infinite array by maintaining only a front pointer. The front pointer tracks the first valid element.

  • Enqueue: Insert at the next available position at the end. No rear pointer is required.
  • Dequeue: Remove the element at front and increment the front pointer.

The space before front is never reused, and unlike basic array implementations, we do not shift elements after each dequeue. This ensures both enqueue and dequeue operations run in O(1) time with a simple design.

Limitations:

  • Wasted space: The elements before the front pointer are never reused, so memory can be wasted if many elements are dequeued.
  • Infinite array assumption: We assume the array is conceptually infinite. In practice, memory is finite, so very large queues can cause memory issues.

Fixed-Size Array Queue

In this article, we will mainly discuss the queue implementation using a fixed-size array. In such an array-based queue, we maintain:

  • A fixed-size array arr[] to store the elements.
  • A variable size to track the current number of elements in the queue.
  • A variable capacity to represent the maximum number of elements the queue can hold.


Operations on Queue

Enqueue (Insert):

  • Add element at the end of the queue if space is available; otherwise, it results in an Overflow condition.
  • Time: O(1) , Space: O(1)

Dequeue:

  • Remove element from the front of the queue; if the queue is empty, it results in an Underflow condition.
  • Time: O(n) (because of shifting) , Space : O(1)

getFront (Peek):

  • Return first element if not empty, else -1.
  • Time: O(1) , Space: O(1)


getRear():

  • Return last element if not empty, else -1.
  • Time: O(1) , Space: O(1)

isEmpty():

  • Checks whether the queue has any elements or not.
  • Returns true if the queue is empty, otherwise false.
  • Time: O(1) , Space: O(1)

isFull():

  • Checks whether the queue has reached its maximum capacity.
  • Returns true if the queue is full, otherwise false.
  • Time: O(1) , Space: O(1)


Full Implementations of Queue using Array


Output
Front: 10
Front: 20
Rear: 30

We can notice that the Dequeue operation is O(n) which is not acceptable. The enqueue and dequeue both operations should have O(1) time complexity. That is why if we wish to implement a queue using array (because of array advantages like cache friendliness and random access), we do circular array implementation of queue.

Comment