Tuesday, July 10, 2018

Program 18


         
ARRAY IMPLEMENTATION OF QUEUE


Write a program to perform insertion and deletion in an array Queue

/* Program to create a Linear Queue using an array to perform insertion, deletion and display operations*/
#include < iostream . h >
#include  < process . h >
#include < conio . h >

# define MAX 10
class Queue
 {
          int queue[ MAX ] ;
          int front, rear ;
     public :
          Queue ( )
           {
                   front = rear = -1 ;  //denotes Q is empty
           }
          void add_to_Q ( )
           {
                   if ( rear == MAX - 1 )
                     {
                             cout<<"Queue is Full  !! " ;
                     }
                   else
                     {
                             int n ;
                             cout << "\nEnter the no to be added:: " ;
                             cin  >> n ;
                             queue [ ++rear ] = n ;
                             if( front == -1 )
                               front = rear;

                             cout<<"\nAdded to the Queue !! " ;
                     }
           }
          void del_from_Q ( )
           {
                   if ( front == -1 )
                    {
                             cout<< " Queue is empty !! Cannot delete !! " ;
                    }
                   else
                    {
                             cout<< "Element deleted :: " << queue [ front ++ ] ;
                    }
           }
          void display_Q ( )
           {
                   if ( front == -1 )
                             cout << "Queue is empty !! ";
                   else
                    {
                             cout<< "\nQueue is ::\t";
                             for ( int i = front ; i <=rear ; ++ i)
                                      cout << queue [ i ] << "\t" ;
                    }
           }
 } Q ;

void main ( )
 {
          int ch ;
          clrscr( ) ;
          while ( 1 )
           {
                   cout << "\n1. Add to Queue ";
                   cout << "\n2. Delete from Queue" ;
                   cout << "\n3. Display Queue" ;
                   cout << "\n4. Exit" ;
                   cout << "\nEnter your choice :: " ;
                   cin  >> ch ;

                   if ( ch == 1 )
                             Q.add_to_Q ( ) ;
                   if ( ch == 2 )
                             Q.del_from_Q ( ) ;
                   if ( ch == 3 )
                             Q. display_Q ( ) ;
                   if ( ch == 4 )
                             exit ( 0 ) ;
                   getch ( ) ;
           }
 }


No comments:

Post a Comment