Introduction
Queue is a linear Data Structure in which the
operations are performed based on FIFO (First In
First Out) principle.
In a Queue always the Insertion operation is done at
“rear” and Deletion operation is done at “front”.
In a Queue sequence of elements entered into the
queue is same as the sequence of elements leave
the queue.
Queue ADT
Definition
• Queue is a linear Data Structure in which the operations
are performed based on FIFO (First In First Out) principle.
Fields
• rear – used to store the position of insertion
• front – used to store the position of deletion
• size – used to store the size of the queue
• element – used to store the value to be inserted
Functions
• enQueue(element) – to insert into queue
• deQueue( ) – to delete from queue
• display( ) – to display all elements in queue
Condition
• Rear >= size – Queue is full
• Front == rear – Queue is empty
class Queue
{
int *Q, front, rear, size;
public:
Queue()
{
cout<<“Enter the Queue size:”;
cin>>size;
Q = new int[size];
front=rear=-1;
}
void qInsert(int);
void qDelete();
void qDisplay();
}
f = front
r = rear
f = r = -1
Q
Queue is EMPTY
0 1 2 3 4 5 . . . SIZE-1
When the object of Queue class is created it allocates memory
and executes the constructor
Insertion
Q
enQueue( int element )
{
if( rear != size-1)
{
rear ++;
Q[rear] = element;
}
}
else
cout<< “Queue is full !!!”;
0 1 2 3 4 5 6 7 8 9
10 20 30 40
Deletion
Q
deQueue( )
{
if( front != rear )
{
front ++;
Cout<<“nElement removed : ”<<Q[front];
}
}
else
cout<< “Queue is empty !!!”;
0 1 2 3 4 5 6 7 8 9
10 20 30 40
Display
Q
if( front != rear )
{
for(int i=front+1; i<=rear, i++)
cout<<Q[front]<<“t”;
}
}
else
cout<< “Queue is empty !!!”;
0 1 2 3 4 5 6 7 8 9
10 20 30 40
display( )
{

Queue by rajanikanth

  • 2.
    Introduction Queue is alinear Data Structure in which the operations are performed based on FIFO (First In First Out) principle. In a Queue always the Insertion operation is done at “rear” and Deletion operation is done at “front”. In a Queue sequence of elements entered into the queue is same as the sequence of elements leave the queue.
  • 3.
    Queue ADT Definition • Queueis a linear Data Structure in which the operations are performed based on FIFO (First In First Out) principle. Fields • rear – used to store the position of insertion • front – used to store the position of deletion • size – used to store the size of the queue • element – used to store the value to be inserted Functions • enQueue(element) – to insert into queue • deQueue( ) – to delete from queue • display( ) – to display all elements in queue Condition • Rear >= size – Queue is full • Front == rear – Queue is empty
  • 4.
    class Queue { int *Q,front, rear, size; public: Queue() { cout<<“Enter the Queue size:”; cin>>size; Q = new int[size]; front=rear=-1; } void qInsert(int); void qDelete(); void qDisplay(); }
  • 5.
    f = front r= rear f = r = -1 Q Queue is EMPTY 0 1 2 3 4 5 . . . SIZE-1 When the object of Queue class is created it allocates memory and executes the constructor
  • 6.
    Insertion Q enQueue( int element) { if( rear != size-1) { rear ++; Q[rear] = element; } } else cout<< “Queue is full !!!”; 0 1 2 3 4 5 6 7 8 9 10 20 30 40
  • 7.
    Deletion Q deQueue( ) { if( front!= rear ) { front ++; Cout<<“nElement removed : ”<<Q[front]; } } else cout<< “Queue is empty !!!”; 0 1 2 3 4 5 6 7 8 9 10 20 30 40
  • 8.
    Display Q if( front !=rear ) { for(int i=front+1; i<=rear, i++) cout<<Q[front]<<“t”; } } else cout<< “Queue is empty !!!”; 0 1 2 3 4 5 6 7 8 9 10 20 30 40 display( ) {