Queue
Queue is a linear data structure
FIFO (First in First Out)
Abstract Data Type
Insertion from Rear(R) and deletion from Front (F) operation
Enqueue - insertion of element
Dequeue - deletion of element
Algorithm for Enqueue and Dequeue operation in circular queue
r : rear
f: front
N: size of queue
index starts with 0 (zero)
Function to check queue state
Algorithm isEmpty()
{
if( f= = -1 && r = = -1)
return true
else
return false
}
Algorithm to insert an element in queue
Algorithm Enqueue()
{
if ( (r+1)% N) = f)
returen overflow
elseif ( isEmpty())
f = r = 0
else
r= (r+1)% N
A[r] = x
}
Algorithm Dequeue()
{
if( (isEmpty() )
{
return underflow
elseif (f= r)
f = r = -1
else
f= (f+1) % N
}
For a given Queue of size N (=5) do following operation:
Enqueue 4, Enqueue 7, Enqueue 9, Dequeue (), Enqueue 2, enqueue 5 , Enqueue 8, Enqueue 6, Dequeue(), Dequeue ()
trace your program using above algorithm.
Feel free to ask any questions, i will try to update solution here. for solutions visit my blog regularly.
Queue is a linear data structure
FIFO (First in First Out)
Abstract Data Type
Insertion from Rear(R) and deletion from Front (F) operation
Enqueue - insertion of element
Dequeue - deletion of element
Algorithm for Enqueue and Dequeue operation in circular queue
r : rear
f: front
N: size of queue
index starts with 0 (zero)
Function to check queue state
Algorithm isEmpty()
{
if( f= = -1 && r = = -1)
return true
else
return false
}
Algorithm to insert an element in queue
Algorithm Enqueue()
{
if ( (r+1)% N) = f)
returen overflow
elseif ( isEmpty())
f = r = 0
else
r= (r+1)% N
A[r] = x
}
Algorithm Dequeue()
{
if( (isEmpty() )
{
return underflow
elseif (f= r)
f = r = -1
else
f= (f+1) % N
}
For a given Queue of size N (=5) do following operation:
Enqueue 4, Enqueue 7, Enqueue 9, Dequeue (), Enqueue 2, enqueue 5 , Enqueue 8, Enqueue 6, Dequeue(), Dequeue ()
trace your program using above algorithm.
Feel free to ask any questions, i will try to update solution here. for solutions visit my blog regularly.
No comments:
Post a Comment