| 1. |
Write algorithms to perform insertion and deletion operations in linear queues, |
|
Answer» (a) Insertion operation It is the process of adding a new item into a queue at the rear end. If the queue is full and we try to add a new item into the queue makes the queue over flow. Algorithm is given below Step 1 : If front = 1 and rear=N or front =rear+1. Then print “OVERFLOW’ and return Step 2 : If front = Null then Set front = 1 and rear =1 Else if rear = N then set rear = 1 Else Set rear = rear + 1 End if Step 3: SetQueue[rear]=item Step 4 : stop (b) Deletion operation It is the process of deleting (removing) a data item from the queue from the front. If the queu is empty and we try to delete an item from the queue makes the queue underflow. Algorithm is given below Step 1 : If front = Null then print “UNDERFLOW and return Step 2 : Set item = Queue[front] Step 3: If front = rear then Set front = Null and rear =Null Else if front = N then set front =1 Else Set front = front +1 End if Step 4 : stop |
|