Thursday, June 27, 2013

Write an algorithm to enqueue(insert, add) an element in a linear Queue.



Let Q[N] be an array implementation of a linear 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=-1 and front=0. Then the algorithm to add an element in the queue is as follows:
1.   Start
2.   Check if the Queue is full or not
if(rear=N-1) THEN
print “Queue is Full” and exit else goto step 3
3.   Increment the rear
++rear;
4.   Add the item at the ‘rear’ position
Q[rear]= item;
5.   Exit

No comments:

Post a Comment