Sunday, June 15, 2008

Queues - Inserting item into queue

Function (Q , item)


This procedure inserts an element ‘item’ at the rear-end of the queue, ‘Q’ only when it is not full. Variable ‘rear’ points to the element recently inserted. Queue overflow condition can be checked by making a call to Function ‘IsFull’.


Step 1 checking overflow condition

call to IsFull.

if (IsFull (Q)) then

message: ‘Queue overflow’

return

else goto step 2

Step 2 setting rear, insert item value

set Q (rear) ¬ Q(rear) + 1

set Q (item [a (rear))] ¬ item.

Step 3 setting front value

if (Q (front) = – 1) then

set Q (front) ® 0

return.

Equivalent C code is given below:

void Enqueue (queue * Q, int data)

{

if (IsFull (Q))

printf (“/n Queue overflow”);

else

{

Q ® rear = Q ® rear + 1;

Q ® item [Q ® rear] = data

if (Q ® front = = – 1)

Q ® front = 0;

}

}


No comments: