Data Structure (2130702)

BE | Semester-3   Summer-2017 | 04/27/2017

Q3) (c)

Write algorithm for inserting and deleting an element in circular queue.

Algorithm for inserting an element in circular queue

Procedure: CQINSERT (F, R, Q, N, Y)

  • This procedure inserts Y at Rear end of the Circular Queue.
  • Queue is represented by a vector Q containing N elements.
  • F is pointer to the Front element of a queue.
  • R is pointer to the Rear element of a queue.
1. [Reset Rear Pointer]
  If R = N
  Then R <-- 1
  Else R <-- R+1
2. [Overflow?]
  If F=R
  Then Write ('Overflow')
    Return
3. [Insert element]
  Q[R] <-- Y
4. [Is Front pointer properly set?]
  If F=0
  Then F<--1
  Return

Algorithm for deleting an element from circular queue

Function CQDELETE (F, R, Q, N)

  • This function deletes & returns an element from Front end of the Circular Queue.
  • Queue is represented by a vector Q containing N elements.
  • F is pointer to the Front element of a queue.
  • R is pointer to the Rear element of a queue.
1. [Underflow?]
  If F = 0
  Then Write (‘UNDERFLOW’)
    Return(0)
2. [Delete Element]
  Y <-- Q[F]
3. [Queue Empty?]
  If F = R
  Then F <-- R <-- 0
    Return(Y)
4. [Increment Front Pointer]
  If F=N
  Then F<--1
  Else F<--F+1
  Return(Y)