Tutorials

Queue


Queue Insertion


We can insert element in the queue from rear end as shown below:

insert item in queue by tutorialsinhand.com

 

From the above diagram we see:

  • When queue is empty, both front and rear are assigned as -1. 
  • During insertion of element the value of rear is incremented by 1. (R=R+1)
  • On insertion of the first element (7), both the front and rear is incremented by 1 (F=R=0). Front will get incremented only once when first element is added. After this front is never increased for insertion operation.
  • During insertion of second element, front remains at 0 while rear is incremented by 1 (R=1)
  • During insertion of third element, front remains at 0 while rear is incremented by 1 (R=2)
  • It will go on for all addition operation. Front will not increase from 0 while rear will keep incrementing by 1.

Algorithm

Given below is the algorithm to insert item at the rear end of the queue.

  1. Initialize Front = -1, Rear = -1 
  2. Repeat step 3 to 7 if  Rear < MaxSize - 1 is true
  3. Read item
  4. if Front == -1 then Front = 0 and Rear =0 
  5. Else Rear = Rear + 1
  6. End if
  7. Set Queue[Rear] = item
  8. Print "Queue overflow"

 



Please Share this page
Views : 33
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!