Thursday, June 27, 2013

Write an algorithm to dequeue(delete, remove) an element from a Circular Queue.



Let Q[N] be an array implementation of a cicular queue, of size N and type T. Then ‘front’ is a variable that point to the front element of the queue and ‘rear’ is a variable that point to the last element of the queue. Initially, rear=N-1 and front=N-1. Then the algorithm to remove an element from the queue is as follows:
1.   Start
2.   Check if the Queue is empty or not
if(rear==front)
  print “Queue is empty” and exit else goto step 3.
3.   if(front==N-1)
  set front=0;
else
  front++;
4.   Store the element in the temporary variable ‘temp’
temp= Q[front];
5.   Return temp.
6.   Exit

No comments:

Post a Comment