Queue is just like the stack with the first in and first out principle. In the stack we say pop and push for adding and deleting an element, but in the case of queue we use words like enqueue and dequeue for entering and deleting any element in a queue.
First In and First Out means that the element which has been entered firstly would be deleted firstly from the queue. for example if we create a queue of 5 element - {2,3,4,5,6}. the first element is called "Front" and last element is called "Rear".
2 3 4 5 6
(Front) (Rear)
So if want to implement the deletion operation the queue then our front element would be deleted first that is 2. if deletion is implemented again then 3 would be deleted and then 4 and so on. this is the concept of first in and first out- that the first entered element is deleted first.
enqueue is entering an element in a queue.
dequeue is deleting an element from a queue.
If we dequeue from a empty queue then its called underflow as there is no element present in the queue.
If we enqueue an element in a full queue then its called overflow as there is not enough space in a full queue to store another element.
For enqueue operation, the time complexity is O(1) as it would be done at the rear element in the queue.
For dequeue operation, the time complexity is O(1) as it would be done on the front element in the queue.
For searching operation, the time complexity would be O(n) as if we want search for element 4 in the above queue, then the searching would start from the front(2), so for n number of elements, the TC would be O(n).
For accessing an element in a queue, the time complexity would be O(n) as to access an element, we have to travel from the first element, so again the TC would be O(n).
0 Comments