- Array representation of the queue two pointer variable such as front and rear, which indicates the position of elements present in the queue of the front and rear ends.
- Array representation of the queue which involves the declaration of one_dimensional array.
- simple queue operations are:
(1)Insert an element at the rear end.
(2)Delete an element at the front end.
(3)Display the element of the queue.
Insert an element at the rear end:
Algorithm: Insert_rear(item)
STEP 1: Check for queue overflow
if(rear==Queue_size-1)
{
print("QUEUE OVERFLOW");
return;
}
STEP 2: Increment rear pointer.
rear=rear+1;
STEP 3: Insert element.
queue[rear]=item;
STEP 4: Stop algorithm.
return;
EXAMPLE:
- Maximum queue size is 5.Assume 4 elements are inserted.
- The queue can insert at most 5 elements & any new elements should be inserted at rear end .it has inserted to the right of the element 40 (ie) queue queue[4] then increment rear pointer by 1.
- Now the rear pointer reaches "Queue_size-1" position so we cannot insert new elements into queue.
Delete an element at the front end:
Algorithm: delete_front()
STEP 1:Check for underflow.
if(front>rear)
{
Printf("Queue is underflow");
return;
}
STEP 2:Delete element.
queue[rear];
STEP 3:Decrease the front pointer.
front=front+1;
STEP 4:Stop algorithm.
return;
EXAMPLE:
- After deleting element 10 from the queue the pointer variable front should be incremented by 1 then front pointer points to the element 20.
Display the elements of the queue:
The elements of the queue can be displayed only if the queue is not empty.
*******
No comments:
Post a Comment