Docstoc

Assignment - C++ Program to illustrate the implementation of arrays as a Linear Queue

Document Sample
Assignment -  C++ Program to illustrate the implementation of arrays as a Linear Queue Powered By Docstoc
					Assignment - C++ Program to illustrate the implementation of arrays as a Linear Queue



**************************************************************************

**************************************************************************/



/*************************************************************************



        By :

               Muhammad Tahir Shahzad [ MTS ]

               B.C.S Honours [ 2000-04 ]

               Government College University Lahore

               Pakistan



  E-mail : mtshome@wol.net.pk



 Web-Site : www.mts-home.cjb.net [ www.wol.net.pk/mtshome ]

               www.mtshome.cjb.net [ www.geocities.com/mtahirshahzad ]



*************************************************************************/



/*************************************************************************/

/*************************************************************************/

/**************************** Header Files *****************************/

/*************************************************************************/

/*************************************************************************/
#include <iostream.h>

#include <conio.h>



/*************************************************************************/

/*************************************************************************/

/****************** Declaration of Constant Variable *******************/

/*************************************************************************/

/*************************************************************************/



const max_length=10;



/*************************************************************************/

/*************************************************************************/

/*********************** Class Definition ******************************/

/*************************************************************************/

/*************************************************************************/



class Queue

 {

     private:

          int queue[max_length];

          int rear;

          int front;
       public:

            Queue( );



            void Delete( );

            void Insert( );

            void print_queue( );

            void show_working( );

  };



/*************************************************************************/

/*************************************************************************/

/*********************** Function Definitions **************************/

/*************************************************************************/

/*************************************************************************/



/*************************************************************************/

//-------------------------- Queue( ) ---------------------------------//

/*************************************************************************/



void Queue::Queue( )

  {

       rear=-1;

       front=-1;



       for(int count=0;count<max_length;count++)
           queue[count]=0;

  }



/*************************************************************************/

//------------------------------- Insert( ) ---------------------------//

/*************************************************************************/



void Queue::Insert( )

  {

      int item;



      cout<<"\n\n\n\n\n\t Enter value to insert into the Queue : ";

      cin>>item;



      if(rear==(max_length-1))

           cout<<"\n\n\t *** Error : Queue is full. \n"<<endl;



      else if(rear==-1 && front==-1)

           {

               rear++;

               queue[rear]=item;

               front=rear;



               cout<<"\n\n\t *** "<<item<<" is inserted into the Queue."<<endl;

           }
      else

             {

                 rear++;

                 queue[rear]=item;



                 cout<<"\n\n\t *** "<<item<<" is inserted into the Queue."<<endl;

             }



      cout<<"\n\n\n\t\t Pres any key to return to Menu. ";



      getch( );

  }



/*************************************************************************/

//--------------------------------- Delete( ) -------------------------//

/*************************************************************************/



void Queue::Delete( )

  {

      if(rear==-1 && front==-1)

             cout<<"\n\n\n\t *** Error : Queue is empty. \n"<<endl;



      else

             {
               cout<<"\n\n\n\t *** "<<queue[front]<<" is deleted from the Queue."<<endl;



               queue[front]=0;



               if(front==rear)

                      front=rear=-1;



               else

                      front++;

           }



      cout<<"\n\n\n\t\t Pres any key to return to Menu. ";



      getch( );

  }



/*************************************************************************/

//--------------------------- print_queue( ) --------------------------//

/*************************************************************************/



void Queue::print_queue( )

  {

      if(front!=-1 && rear!=-1)

           {

               cout<<"\n\n\n\n\n\t Values inserted into the Queue are : \n"<<endl;
                for(int count=front;count<=rear;count++)

                      cout<<"\t Stack ["<<count<<"] = "<<queue[count]<<endl;

            }



     else

            cout<<"\n\n\n\n\n\t *** Nothing to show. "<<endl;



     cout<<"\n\n\n\t\t Pres any key to return to Menu. ";



     getch( );

 }




/*************************************************************************/

//------------------------- show_working( ) ---------------------------//

/*************************************************************************/



void Queue::show_working( )

 {

     char Key=NULL;



     do

            {

                clrscr( );
gotoxy(5,5);

cout<<"********** Implementation of Array as Queue **********"<<endl;



gotoxy(10,8);

cout<<"Select one of the listed operation :"<<endl;



gotoxy(15,10);

cout<<"- Press \'I\' to Insert a value"<<endl;



gotoxy(15,12);

cout<<"- Press \'D\' to Delete a value"<<endl;



gotoxy(15,14);

cout<<"- Press \'P\' to Print the values"<<endl;



gotoxy(15,16);

cout<<"- Press \'E\' to Exit"<<endl;



Input:



gotoxy(10,20);

cout<<"              ";



gotoxy(10,20);
               cout<<"Enter your Choice : ";



               Key=getche( );



               if(int(Key)==27 || Key=='e' || Key=='E')

                      break;



               else if(Key=='i' || Key=='I')

                      Insert( );



               else if(Key=='d' || Key=='D')

                      Delete( );



               else if(Key=='p' || Key=='P')

                      print_queue( );



               else

                      goto Input;

           }

      while(1);

  }



/*************************************************************************/

/*************************************************************************/

//---------------------------- main( ) --------------------------------//
/*************************************************************************/

/*************************************************************************/



int main( )

  {

      Queue obj;



      obj.show_working( );



      return 0;

  }



/*************************************************************************/

/*************************************************************************/

//---------------------------- THE END --------------------------------//

/*************************************************************************/

/*************************************************************************/

				
DOCUMENT INFO
Shared By:
Stats:
views:64
posted:2/10/2010
language:English
pages:10
Description: Assignment - C++ Program to illustrate the implementation of arrays as a Linear Queue