Write an algorithm to implement the Queue ADT using arrays.

Definition of Queue ADT: Queue is an ordered collection of data such that the data is inserted at one end and deleted from other end. It is a collection of items to be processed on a First-In-First-Out(FIFO) or First Come First Served(FCFS) basics.



Basic Operation Associated on Queues:
1) Insert an item into the Queue.
2) Delete an item into the Queue.

a) Algorithm to insert an item into a Queue Q:
Procudure Insert(Q, SIZE, F, R, ITEM)
Q Array
SIZE Queue size
F front Pointer
R rear pointer
ITEM: information to be inserted at the rear of queue.
Step 1: {Check for Queue overflow}
If R>=SIZE then
Prints(‘Queue overflow’)
Return
Step 2: {Increment rear pointer}
R=R+1
Step 3: {Insert new element at rear end of queue}
Q[R]=ITEM
Step 4: {If initially the queue is empty adjust the front pointer}
If F=0, then F=1

b) Algorithm to delete an item from a Queue Q:
Procedure Delete(Q, F, R)
Q Array
F front Pointer
R rear pointer


ITEM: information to be inserted at the rear of queue.
Step 1: {Check for Queue underflow}
If F=0 then
Prints(‘Queue underflow’)
Return
Step 2: {Delete the queue element at front end and store it into item}
ITEM=Q[F]
Step 3: {If queue is empty after deletion,set front and rear pointers to 0}
If F=R then
F=0
R=0
{Otherwise increment front pointer}
Else
F=F+1
Return(ITEM)

c) Algorithm to display the items from the Queue Q
Procedure Dispaly(Q)
Q Array
Step1: {Check queue values}
If F<0
Prints(‘Queue is empty’)
Step 2: {display Queue values}
For I value F to R
Prints(Q[I])
I=I+1


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments:

Thanks
Mukesh Rajput