Queues
Queue is an abstract data type or a linear data structure in which the first element is inserted from one end called the TAIL, and the removal of existing element takes place from the other end called as HEAD.
This makes queue as FIFO(First in First Out) data structure (that element inserted first will be removed first).
The process to add an element into queue is called Enqueue and the process of removal of an element from queue is called Dequeue.
ENQUEUE:
- Check if the queue is full or not.
- If the queue is full, then print overflow error and exit the program.
- If the queue is not full, then increment the tail and add the element.
DEQUEUE:
- Check if the queue is empty or not.
- If the queue is empty, then print underflow error and exit the program.
- If the queue is not empty, then print the element at the head and increment the head.
peek( ) function is oftenly used to return the value of first element without dequeuing it.
NOTE:Like stack, queue is also an ordered list of elements of similar data types.
Applications of Queue:
Serving requests on a single shared resource, such as a printer, and CPU task scheduling .
In real life scenario, Call Center phone systems uses Queues to hold people calling them in an order, until a service representative is free.
Handling of interrupts in real-time systems (first come first served).
Implementation of Queue Data Structure:
Queue can be implemented using an Array, Stack or Linked List.
Array:
The easiest way of implementing a queue is by using an array to make a policy decision that element zero of the array (array[0]) is initially the head and the tail of the queue.
As we add elements to the queue, the tail keeps on moving ahead, always pointing to the position where the next element will be inserted, while the head remains at the first index.
When we remove an element from Queue, we can follow two possible approaches.
In the first approach, we remove the element at head position, and then one by one shift all the other elements in forward position.
In the second approach we remove the element from head position and then move head to the next position.
References:
https://www.studytonight.com/data-structures/queue-data-structure
Awesome article on queues, mate! This deserves a resteem. Keep it up!
thank you for encouragement.