The time required to insert in the Queue is ?
A. O(n)
B. O(n2)
C. O(1)
D. O(log n)
Answer : C. O(1)
Explanation :
- Queues offer random access to their contents by shifting the first element off the front of the queue. You have to do this repeatedly to access an arbitrary element somewhere in the queue. Therefore, access is O(n).
- In the queue only two operations are allowed enqueue and dequeue.
- Enqueue means to insert an item into the back of the queue.
- Dequeue means removing the front item.
Complexity Analysis of Queue Operations :
- Enqueue : O(1)
- Dequeue : O(1)