Queue:
A Queue is a linear data structure which follows First In First Out (FIFO) principle, in which insertion is performed at rear end deletion is performed at front end.
(or)
A queue is a "waiting line" type of data structure. Much like with a stack, we can insert items into a queue and remove items from a queue. However, a queue has "first come, first served" behavior in that the first item inserted into the queue is the first one removed.
Operations on Queue:
- Enqueue
- Dequeue
Exception condition:
Overflow: Attempt to insert an element, when the queue is full.
Underflow: Attempt to delete an element from the queue, when the queue is empty.
Uses of Queues:
Queues are commonly used in operating systems and other software where some sort of waiting line has to be maintained for obtaining access to a resource. For example, an operating system may keep a queue of processes that are waiting to run on the CPU. It might also keep a queue of print jobs that are waiting to be printed on a printer. Queues are also used in simulations of stores and their waiting lines at the check-out counters.
Alogarithm Steps:
Step 1: Initialize the queue variables front =0 and rear = -1
Step 2: Read the queue operation type.
Step 3: Check the queue operations status.
i). If it is Insertion then do the following steps
- Check rear < queue_size is true increment the rear by one and read the queue element and also display queue. other wise display the queue is full.
- Go to step2.
ii). If it is deletion then do the following steps
- Check rear< front is true then display the queue is empty.
- Move the elements to one step forward (i.e. move to previous index ).
- Decreases the rear value by one (rear=rear-1).
- Display queue
- Go to step2.
C Program To Implement Queue Using Array
CPP Program To Implement Queue Using Array
|