Queue - Introduction
- Follows First In First Out (FIFO)
- Implementation Options of queue :
- Arrays
- Linear queue
- Circular queue
- Linked List
- Linear queue (only, and not Circular queue)
Why we need circular queue ?
Because in the array implementation of linear queue, when we perform deque operation, it causes few blank cells in linear queue (at the start of queue). Due to this, we waste a lot of space.
Also, we do not want to shift the elements in the array to left (or start) so as to prevent empty cells at start as it will take O(n) time. We have to do it in O(1) time. For doing that we can again add new elements to the start and at the same time maintaining two pointers i.e. startOfQueue and endOfQueue. (See actual code implementation of circular queue using arrays)
When to use queue / Advantages of queue ?
1. When we want to access data in FIFO order.
2. Data is not easily corrupted as the insertion and deletion of the elements in queue is done in a particular fashion.
When to avoid queue / Disadvantages of Queue ?
Random access is not possible. If we have done a mistake while enqueueing, it is very difficult to rectify it.
When to use queue / Advantages of queue ?
1. When we want to access data in FIFO order.
2. Data is not easily corrupted as the insertion and deletion of the elements in queue is done in a particular fashion.
When to avoid queue / Disadvantages of Queue ?
Random access is not possible. If we have done a mistake while enqueueing, it is very difficult to rectify it.
Comments
Post a Comment