Queue in Data Structure(DS)
Queue
Queues are data structures that follow the First In First Out (FIFO) i.e. the first element that is added to the queue is the first one to be removed. Elements are always added to the back(rear) and removed from the front. Think of it as a line of people waiting for a bus. The person who is at the beginning of the line is the first one to enter the bus.
Basic features of Queue
- Like a stack, the queue is also an ordered list of elements of similar data types.
- A queue is a FIFO( First in First Out ) structure.
- Once a new element is inserted into the Queue, all the elements inserted before the new element in the queue
- must be removed, to remove the new element.
- In a simple queue, there is a disadvantage after deleting element we cannot insert the element in the queue because of insertion is possible from the rear but deletion is performed from the front.
insertion
--------------------->
Rear ---------------------> Front
Deletion
Algorithm for Insert Operation
Step 1: If REAR >= SIZE – 1 then
Write “Queue is Overflow”
Step 2: REAR = REAR + 1
Step 3: QUEUE [REAR] = X
Step 4: If FRONT = -1 then
FRONT = 0
Algorithm for Delete Operation
Step 1: If FRONT = -1 then
Write “Queue is Underflow”
Step 2: Return QUEUE [FRONT]
Step 3: If FRONT = REAR then
FRONT = 0
REAR = 0
Else
FRONT = FRONT + 1
Comments
Post a Comment