- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Time and Space Complexity Analysis of Queue operations
Introduction
Queue is a linear data structure that uses the FIFO approach for inserting and removing its elements. It can be implemented by using arrays and linked lists. In this tutorial, we will analyze the time and space complexity of array based queue for its different operation.
Queue Implementation Using Array
The principle of Queue is its FIFO approach and it states that the element that enters first in the Queue will be the first to be removed from it. Its elements are inserted at the Rear end. Queue elements are removed from the Front end. The real-life example of Queue is a Queue in front of the ticket window.
We can implement a Queue using array and linked-list. In an array based queue, the queue size is defined initially.
The important functions of the queue are
enQueue() − It is used to insert data in the Queue.
deQueue() − It is used to remove data from the Queue.
isEmpty() − It will check whether the queue is empty or not. It returns a boolean value.
peek() − Returns the peak value of the queue.
Time Complexity − It is the amount of time used to execute.
Space Complexity − It is the memory utilization.
Example 1
Analyzing time and Space Complexities of different Queue Operations in C/C++
#include <iostream> using namespace std; #define capacity 10 class Queue { public: int queue[capacity]; int front; int rear; Queue() { front = -1; rear = -1; } void enqueue(int val1) { if (front == -1) { front++; } if (rear == capacity - 1) { cout << "Queue is overflow!!!
"; return; } queue[++rear] = val1; cout << " Successful insertion of " << val1 <<"
"; } }; int main() { Queue aq; //using enQueue() to insert 5 and 7 in the queue aq.enqueue(5); aq.enqueue(7); return 0; }
Output
Successful insertion of 5 Successful insertion of 7
Analyzing the Complexities
Time Complexity: The time complexity of enQueue() operation using array is O(1).
Space Complexity: It is constant and no extra space is used. It is O(1)
Example 2
Finding Time and Space Complexity Analysis of Queue operations using deQueue() operation in C/C++
#include <iostream> using namespace std; #define capacity 10 class Queue { public: int queue[capacity]; int last; int first; Queue() { last = -1; first = -1; } void enqueue(int val1) { if (last == -1) { last++; } if (first == capacity - 1) { cout << "Queue overflow!!!
"; return; } queue[++first] = val1; } void dequeue() { if (last == -1 || last > first) { cout << "Queue is empty!!!
"; return; } cout << "Element deleted from queue is : " << queue[last++] << "
"; } }; int main() { Queue aq; //Inserting Queue elements aq.enqueue(7); aq.enqueue(2); //removing Queue element aq.dequeue(); return 0; }
Output
Element deleted from Queue is 7
Analyzing Complexities
Time Complexity: O(1): it is because of the use of a single arithmetic operator.
Space Complexity: O(1)
Example 3
Finding Time and Space Complexity Analysis using peek() operation of the Queue in C/C++
#include <iostream> using namespace std; #define capacity 10 class Queue { public: int queue[capacity]; int last; int first; Queue() { last = -1; first = -1; } void enqueue(int v) { if (last == -1) { last++; } if (first == capacity - 1) { cout << "Queue is overflowed!!!
"; return; } queue[++first] = v; } void peek() { if (last == -1 || last > first) { cout << "Queue is empty !
"; return; } cout << "Front Element of the Queue is : " << queue[last] << "
"; } }; int main(){ Queue aq; aq.enqueue(16); aq.enqueue(20); //it will give the first element of the Queue aq.peek(); return 0; }
Output
Front Element of the Queue is : 16
Complexities Analysis
Time Complexity = O(1)
Space Complexity = O(1)
Example 4
Finding Time and Space Complexity Analysis of Queue operations using isempty() Operation in C/C++
#include <iostream> using namespace std; #define capacity 10 class Queue { public: int queue[capacity]; int head; int tail; Queue() { head = -1; tail = -1; } bool isempty() { if (head == -1 || head > tail) { return 1; } return 0; } }; int main() { Queue aq; if (aq.isempty()) { cout << "It is empty queue
"; } else cout << "Queue has space
"; return 0; }
Output
It is empty Queue
Complexity Analysis
Time Complexity = O(1). It checks the elements of the Queue, that is constant.
Space Complexity = O(1)
Conclusion
In this tutorial, we analyze the time and space complexities of array queues for enQueue(), dequeue (), peek(), and isempty() operations. We find that they all have the same time complexity of O(1) as they are using only one operation to execute the methods.
The Space complexity of all operations is also the same. It is because they use fixed memory and utilize no memory beyond that.