# Time and Space Complexity Analysis of Queue operations

• Difficulty Level : Medium
• Last Updated : 24 Aug, 2022

## What is Queue?

Queue is a linear data structure that follows FIFO approach (First In First Out). One can imagine a queue as a line of people waiting in sequential order which starts from the beginning of the line. It is an ordered list in which insertions are done at one end which is known as the rear and deletions are done from the other end known as the front. A good example of a queue is any queue of consumers for a resource where the consumer that came first is served first. A queue can be implemented using Arrays or Linked Lists.

## Complexity analysis of different Queue operations:

### 1) enqueue():

This operation inserts an element at the back of the queue. It takes one parameter, the value that is to be inserted at the back of the queue.

Below is the implementation of enqueue() using Array:

## C++

 `#include ``using` `namespace` `std;``#define capacity 10``class` `Queue {``public``:``    ``int` `queue[capacity];``    ``int` `front;``    ``int` `rear;`` ` `    ``Queue()``    ``{``        ``front = -1;``        ``rear = -1;``    ``}`` ` `    ``void` `enqueue(``int` `val)``    ``{``        ``if` `(front == -1) {``            ``front++;``        ``}`` ` `        ``if` `(rear == capacity - 1) {``            ``cout << ``"Queue overflow!!!\n"``;``            ``return``;``        ``}`` ` `        ``queue[++rear] = val;``        ``cout << val << ``" inserted successfully\n"``;``    ``}``};``int` `main()``{``    ``Queue q;`` ` `    ``// Inserting elements in the queue``    ``// using enqueue operation``    ``q.enqueue(1);``    ``q.enqueue(2);`` ` `    ``return` `0;``}`

Output

```1 inserted successfully
2 inserted successfully```

Complexity Analysis:

• Time Complexity: O(1), In enqueue function a single element is inserted at the last position. This takes a single memory allocation operation which is done in constant time.
• Auxiliary Space: O(1). As no extra space is being used.

Below is the implementation of enqueue() using Linked List :

## C++

 `#include ``using` `namespace` `std;``class` `node {``public``:``    ``int` `data;``    ``node* next;`` ` `    ``node(``int` `val)``    ``{``        ``data = val;``        ``next = NULL;``    ``}``};``class` `Queue {``public``:``    ``node* front;``    ``node* rear;`` ` `    ``Queue()``    ``{``        ``front = rear = NULL;``    ``}`` ` `    ``void` `enqueue(``int` `val)``    ``{``        ``// if queue is empty``        ``if` `(rear == NULL) {``            ``// Create a new node as rear``            ``rear = ``new` `node(val);``            ``rear->next = NULL;``            ``rear->data = val;`` ` `            ``// Front will be rear as only``            ``// one element exist in queue``            ``front = rear;``        ``}``        ``else` `{``            ``// Create temp node of val value``            ``node* temp = ``new` `node(val);`` ` `            ``// Add temp after the rear of queue``            ``rear->next = temp;`` ` `            ``// Update temp as the end element``            ``rear = temp;``        ``}``        ``cout << val << ``" inserted successfully \n"``;``    ``}``};``int` `main()``{``    ``Queue q;`` ` `    ``// Inserting elements in the queue``    ``// using enqueue operation``    ``q.enqueue(1);``    ``q.enqueue(2);`` ` `    ``return` `0;``}`

Output

```1 inserted successfully
2 inserted successfully ```

Complexity Analysis:

• Time Complexity: O(1). Only a new node is created and the pointer of the last node is updated. This includes only memory allocation operations. Hence it can be said that insertion is done in constant time.
• Auxiliary Space: O(1). No extra space is used.

### 2) dequeue():

This operation removes an element present at the front of the queue. Also, it results in an error if the queue is empty.

Below is the implementation of dequeue() using Array :

## C++

 `#include ``using` `namespace` `std;``#define capacity 10``class` `Queue {``public``:``    ``int` `queue[capacity];``    ``int` `front;``    ``int` `rear;`` ` `    ``Queue()``    ``{``        ``front = -1;``        ``rear = -1;``    ``}`` ` `    ``void` `enqueue(``int` `val)``    ``{``        ``if` `(front == -1) {``            ``front++;``        ``}`` ` `        ``if` `(rear == capacity - 1) {``            ``cout << ``"Queue overflow!!!\n"``;``            ``return``;``        ``}`` ` `        ``queue[++rear] = val;``    ``}``    ``void` `dequeue()``    ``{``        ``if` `(front == -1 || front > rear) {``            ``cout << ``"Queue is empty!!!\n"``;``            ``return``;``        ``}`` ` `        ``cout << ``"Element deleted from queue : "` `<< queue[front++] << ``"\n"``;``    ``}``};``int` `main()``{``    ``Queue q;`` ` `    ``// Inserting elements in the queue``    ``// using enqueue operation``    ``q.enqueue(1);``    ``q.enqueue(2);`` ` `    ``// Deleting elements from the queue``    ``// using dequeue operation``    ``q.dequeue();`` ` `    ``return` `0;``}`

Output

`Element deleted from queue : 1`

Complexity Analysis:

• Time Complexity: O(1). In array implementation, only an arithmetic operation is performed i.e., the front pointer is incremented by 1. This is a constant time function.
• Auxiliary Space: O(1). No extra space is utilized for deleting an element from the queue.

Below is the implementation of dequeue using Linked List :

## C++

 `#include ``using` `namespace` `std;``#define capacity 10``class` `node {``public``:``    ``int` `data;``    ``node* next;`` ` `    ``node(``int` `val)``    ``{``        ``data = val;``        ``next = NULL;``    ``}``};``class` `Queue {``public``:``    ``node* front;``    ``node* rear;`` ` `    ``Queue()``    ``{``        ``front = rear = NULL;``    ``}`` ` `    ``void` `enqueue(``int` `val)``    ``{``        ``// if queue is empty``        ``if` `(rear == NULL) {``            ``// Create a new node as rear``            ``rear = ``new` `node(val);``            ``rear->next = NULL;``            ``rear->data = val;`` ` `            ``// Front will be rear as only``            ``// one element exist in queue``            ``front = rear;``        ``}``        ``else` `{``            ``// Create temp node of val value``            ``node* temp = ``new` `node(val);`` ` `            ``// Add temp after the rear of queue``            ``rear->next = temp;`` ` `            ``// Update temp as the end element``            ``rear = temp;``        ``}``    ``}`` ` `    ``void` `dequeue()``    ``{``        ``// point temp to front of queue``        ``node* temp = front;``        ``// if queue is empty``        ``if` `(front == NULL) {``            ``cout << ``"Underflow"` `<< endl;``            ``return``;``        ``}``        ``else` `if` `(temp->next != NULL) {``            ``temp = temp->next;``            ``cout << ``"Element deleted from queue is : "` `<< front->data << endl;``            ``free``(front);``            ``front = temp;``        ``}``        ``// if queue consist of only one element``        ``else` `{``            ``cout << ``"Element deleted from queue is : "` `<< front->data << endl;``            ``free``(front);``            ``front = NULL;``            ``rear = NULL;``        ``}``    ``}``};``int` `main()``{``    ``Queue q;`` ` `    ``// Inserting elements using``    ``// enqueue operation``    ``q.enqueue(5);``    ``q.enqueue(7);`` ` `    ``// Removing elements from queue``    ``// using dequeue operation``    ``q.dequeue();`` ` `    ``return` `0;``}`

Output

`Element deleted from queue is : 5`

Complexity Analysis:

• Time Complexity: O(1). In dequeue operation, only the first node is deleted and the front pointer is updated. This is a constant time operation.
• Auxiliary Space: O(1). No extra space is utilized for deleting an element from the queue.

### 3) peek():

This operation prints the element present at the front of the queue.

Below is the implementation of peek() using Array:

## C++

 `#include ``using` `namespace` `std;``#define capacity 10``class` `Queue {``public``:``    ``int` `queue[capacity];``    ``int` `front;``    ``int` `rear;`` ` `    ``Queue()``    ``{``        ``front = -1;``        ``rear = -1;``    ``}`` ` `    ``void` `enqueue(``int` `val)``    ``{``        ``if` `(front == -1) {``            ``front++;``        ``}`` ` `        ``if` `(rear == capacity - 1) {``            ``cout << ``"Queue overflow!!!\n"``;``            ``return``;``        ``}`` ` `        ``queue[++rear] = val;``    ``}`` ` `    ``void` `peek()``    ``{``        ``if` `(front == -1 || front > rear) {``            ``cout << ``"Queue is empty !\n"``;``            ``return``;``        ``}`` ` `        ``cout << ``"Element at the front of queue: "` `<< queue[front] << ``"\n"``;``    ``}``};``int` `main()``{``    ``Queue q;`` ` `    ``// Inserting elements in the queue``    ``// using enqueue operation``    ``q.enqueue(1);``    ``q.enqueue(2);`` ` `    ``// Printing front element``    ``// using peek operation``    ``q.peek();`` ` `    ``return` `0;``}`

Output

`Element at the front of queue: 1`

Complexity Analysis:

• Time Complexity: O(1). In this operation, only a memory address is accessed. This is a constant time operation.
• Auxiliary Space: O(1). No extra space is utilized to access the first value.

Below is the implementation of peek() using Linked List:

## C++

 `#include ``using` `namespace` `std;``#define capacity 10``class` `node {``public``:``    ``int` `data;``    ``node* next;`` ` `    ``node(``int` `val)``    ``{``        ``data = val;``        ``next = NULL;``    ``}``};``class` `Queue {``public``:``    ``node* front;``    ``node* rear;`` ` `    ``Queue()``    ``{``        ``front = rear = NULL;``    ``}`` ` `    ``void` `enqueue(``int` `val)``    ``{``        ``// if queue is empty``        ``if` `(rear == NULL) {``            ``// Create a new node as rear``            ``rear = ``new` `node(val);``            ``rear->next = NULL;``            ``rear->data = val;`` ` `            ``// Front will be rear as only``            ``// one element exist in queue``            ``front = rear;``        ``}``        ``else` `{``            ``// Create temp node of val value``            ``node* temp = ``new` `node(val);`` ` `            ``// Add temp after the rear of queue``            ``rear->next = temp;`` ` `            ``// Update temp as the end element``            ``rear = temp;``        ``}``    ``}`` ` `    ``void` `peek()``    ``{``        ``// if queue is empty``        ``if` `(front == NULL) {``            ``cout << ``"Queue is empty!!!"` `<< endl;``        ``}``        ``else` `{``            ``// return value of front``            ``cout << ``"Element present at the front of queue: "` `<< front->data << ``"\n"``;``        ``}``    ``}``};``int` `main()``{``    ``Queue q;`` ` `    ``// Inserting elements using``    ``// enqueue operation``    ``q.enqueue(5);``    ``q.enqueue(7);`` ` `    ``// Front element using``    ``// peek operation``    ``q.peek();`` ` `    ``return` `0;``}`

Output

`Element present at the front of queue: 5`

Complexity Analysis:

• Time Complexity: O(1). In linked list implementation also a single memory address is accessed. It takes constant time.
• Auxiliary Space: O(1). No extra space is utilized to access the first element.

### 4) initialize():

This operation takes an array and adds the element at the back of the Queue.

Implementation of initialize() using array:

## C++

 `#include ``using` `namespace` `std;``#define capacity 10``class` `Queue {``public``:``    ``int` `queue[capacity];``    ``int` `front;``    ``int` `rear;`` ` `    ``Queue()``    ``{``        ``front = -1;``        ``rear = -1;``    ``}`` ` `    ``void` `enqueue(``int` `val)``    ``{``        ``if` `(front == -1) {``            ``front++;``        ``}`` ` `        ``if` `(rear == capacity - 1) {``            ``cout << ``"Queue overflow !\n"``;``            ``return``;``        ``}`` ` `        ``queue[++rear] = val;``    ``}``    ``void` `initialize(``int` `arr[], ``int` `N)``    ``{`` ` `        ``for` `(``int` `i = 0; i < N; i++) {``            ``// Value to be inserted``            ``int` `val = arr[i];`` ` `            ``// Inserting using enqueue``            ``enqueue(val);``        ``}`` ` `        ``// Printing the queue``        ``for` `(``int` `i = front; i <= rear; i++) {``            ``cout << queue[i] << ``" "``;``        ``}``    ``}``};`` ` `// Driver code``int` `main()``{``    ``Queue q;`` ` `    ``int` `arr[] = { 2, 4, 7, 9, 1 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);`` ` `    ``// Calling the initialize function``    ``q.initialize(arr, N);`` ` `    ``return` `0;``}`

Output

`2 4 7 9 1 `

Complexity Analysis:

• Time Complexity: O(N). Inserting each element is a constant time operation. So for inserting N elements N unit of time is required.
• Auxiliary Space: O(N). N elements are inserted.

## C++

 `#include ``using` `namespace` `std;``class` `node {``public``:``    ``int` `data;``    ``node* next;`` ` `    ``node(``int` `val)``    ``{``        ``data = val;``        ``next = NULL;``    ``}``};``class` `Queue {``public``:``    ``node* front;``    ``node* rear;`` ` `    ``Queue()``    ``{``        ``front = rear = NULL;``    ``}`` ` `    ``void` `enqueue(``int` `val)``    ``{``        ``// if queue is empty``        ``if` `(rear == NULL) {``            ``// Create a new node as rear``            ``rear = ``new` `node(val);``            ``rear->next = NULL;``            ``rear->data = val;`` ` `            ``// Front will be rear as only``            ``// one element exist in queue``            ``front = rear;``        ``}``        ``else` `{``            ``// Create temp node of val value``            ``node* temp = ``new` `node(val);`` ` `            ``// Add temp after the rear of queue``            ``rear->next = temp;`` ` `            ``// Update temp as the end element``            ``rear = temp;``        ``}``    ``}``    ``void` `initialize(``int` `arr[], ``int` `N)``    ``{`` ` `        ``for` `(``int` `i = 0; i < N; i++) {``            ``// Value to be inserted``            ``int` `val = arr[i];`` ` `            ``// Inserting using enqueue``            ``enqueue(val);``        ``}`` ` `        ``node* temp = front;``        ``// Printing the queue``        ``while` `(temp != NULL) {``            ``cout << temp->data << ``" "``;``            ``temp = temp->next;``        ``}``    ``}``};`` ` `// Driver code``int` `main()``{``    ``Queue q;`` ` `    ``int` `arr[] = { 2, 8, 7, 3, 1 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);`` ` `    ``// Calling the initialize function``    ``q.initialize(arr, N);`` ` `    ``return` `0;``}`

Output

`2 8 7 3 1 `

Complexity Analysis:

• Time Complexity: O(N). Creating a new node and making a link takes unit time. So to insert N elements (i.e., creating N nodes and linking them) N unit of times is required.
• Auxiliary Space: O(N). N elements need to be inserted.

### 5) isfull():

Function that returns true if the queue is filled completely else returns false.

Below is the implementation of isfull() using array:

## C++

 `#include ``using` `namespace` `std;``#define capacity 10``class` `Queue {``public``:``    ``int` `queue[capacity];``    ``int` `front;``    ``int` `rear;`` ` `    ``Queue()``    ``{``        ``front = -1;``        ``rear = -1;``    ``}`` ` `    ``bool` `isfull()``    ``{``        ``if` `(rear == capacity - 1)``            ``return` `1;`` ` `        ``return` `0;``    ``}``};``int` `main()``{``    ``Queue q;`` ` `    ``if` `(q.isfull()) {``        ``cout << ``"Queue is filled\n"``;``    ``}``    ``else` `{``        ``cout << ``"Queue is not filled completely\n"``;``    ``}``    ``return` `0;``}`

Output

`Queue is not filled completely`

Complexity Analysis:

• Time Complexity: O(1). It only performs an arithmetic operation to check if the queue is full or not.
• Auxiliary Space: O(1). It requires no extra space.

Below is the implementation of isfull() using Linked List:

## C++

 `#include ``using` `namespace` `std;``#define capacity 10``class` `node {``public``:``    ``int` `data;``    ``node* next;`` ` `    ``node(``int` `val)``    ``{``        ``data = val;``        ``next = NULL;``    ``}``};``class` `Queue {``public``:``    ``node* front;``    ``node* rear;`` ` `    ``Queue()``    ``{``        ``front = rear = NULL;``    ``}`` ` `    ``bool` `isfull()``    ``{``        ``// to store current length of queue``        ``int` `length = 0;`` ` `        ``// temp pointing to front node``        ``node* temp = front;`` ` `        ``// if queue is empty``        ``if` `(temp == NULL)``            ``return` `0;`` ` `        ``while` `(temp->next != NULL) {``            ``length++;``            ``temp = temp->next;``        ``}`` ` `        ``// if queue size is same as maximum capacity``        ``if` `(length == capacity) {``            ``return` `1;``        ``}`` ` `        ``return` `0;``    ``}``};``int` `main()``{``    ``Queue q;`` ` `    ``if` `(q.isfull()) {``        ``cout << ``"Queue is filled\n"``;``    ``}``    ``else` `{``        ``cout << ``"Queue is not filled completely\n"``;``    ``}`` ` `    ``return` `0;``}`

Output

`Queue is not filled completely`

Complexity Analysis:

• Time Complexity: O(N). The whole linked list is traversed to calculate the length and then the length is checked with the capacity. The traversal of the linked list takes O(N) time.
• Auxiliary Space: O(1). No extra space is required.

### 6) isempty():

Function that returns true if the queue is empty else returns false.

Below is the implementation of isempty() operation using array:

## C++

 `#include ``using` `namespace` `std;``#define capacity 10``class` `Queue {``public``:``    ``int` `queue[capacity];``    ``int` `front;``    ``int` `rear;`` ` `    ``Queue()``    ``{``        ``front = -1;``        ``rear = -1;``    ``}`` ` `    ``bool` `isempty()``    ``{``        ``// if there are no elements or``        ``// the queue has exceed its rear``        ``if` `(front == -1 || front > rear) {``            ``return` `1;``        ``}`` ` `        ``return` `0;``    ``}``};``int` `main()``{``    ``Queue q;`` ` `    ``if` `(q.isempty()) {``        ``cout << ``"Queue is empty\n"``;``    ``}``    ``else` `{``        ``cout << ``"Queue is not empty \n"``;``    ``}`` ` `    ``return` `0;``}`

Output

`Queue is empty`

Complexity Analysis:

• Time Complexity: O(1) It only checks the position stored in the first and last pointer
• Auxiliary Space: O(1) NO extra space is required to check the values of the first and the last pointer.

Below is the implementation of isempty() operation using LinkedList:

## C++

 `#include ``using` `namespace` `std;``#define capacity 10``class` `node {``public``:``    ``int` `data;``    ``node* next;`` ` `    ``node(``int` `val)``    ``{``        ``data = val;``        ``next = NULL;``    ``}``};``class` `Queue {``public``:``    ``node* front;``    ``node* rear;`` ` `    ``Queue()``    ``{``        ``front = rear = NULL;``    ``}`` ` `    ``bool` `isempty()``    ``{``        ``// if queue has 0 nodes``        ``if` `(front == NULL) {``            ``return` `1;``        ``}`` ` `        ``return` `0;``    ``}``};``int` `main()``{``    ``Queue q;`` ` `    ``if` `(q.isempty()) {``        ``cout << ``"Queue is filled\n"``;``    ``}``    ``else` `{``        ``cout << ``"Queue is not filled completely\n"``;``    ``}`` ` `    ``return` `0;``}`

Output

`Queue is filled`

Complexity Analysis:

• Time Complexity: O(1), It checks if the pointer of first is Null or not. This operation takes constant time.
• Auxiliary Space: O(1). No extra space is required.

My Personal Notes arrow_drop_up