Thursday, 11 February 2016

C++ program for Queue Implementation using Class and Linked list



/* Program to implement the queue-operation through linked list */


#include <iostream>
#include <graphics.h>

 using namespace std;

 class QUEUE;    //declaration of class Queue
 typedef QUEUE *NODE;

 //class used for the queue implementation
 class QUEUE
 {

     public :
         int data;    //store the data element for queue
         QUEUE *next; //pointer of QUEUE type to point to next node of queue
        
         NODE INSERT_DATA( int , NODE );
         NODE DELETE_DATA( NODE );
         void SHOW_DATA( NODE );

 }calling; //object to call the MF of its class


 //public MF of QUEUE to perform-
 //data insertion in queue by adding-
 //a new node
 NODE QUEUE :: INSERT_DATA( int value , NODE first )
 {

      NODE temp;
     temp=new QUEUE;
     if(first == NULL) {
         temp->data = value;
         temp->next = first;
     }
   
     else {
     temp->data=value;
     first->next=temp;
     temp->next= NULL;
     }
    
     return temp;
 }


 //MF to perform data deletion of -
 //a node from the queue
 NODE QUEUE :: DELETE_DATA( NODE first )
 {

      NODE temp;
     temp = new QUEUE;
     temp = first;
     if( first != NULL )
         temp = first->next;

     delete first;
     return temp;

 }


 //MF to perform the display of -
 //data in each node of queue
 void QUEUE :: SHOW_DATA( NODE first )
 {
      NODE temp;
     temp = new QUEUE;
     temp = first;
     if( temp == NULL )
         cout<<"\n\nright now Queue is EMPTY !";

     else {
         cout<<"\n\tQUEUE at present :\n\n\t\tFIRST-->\t<- ";
         while( temp != NULL ) {
             cout<<"|"<<temp->data
                 <<"| <- ";
                
             temp = temp->next;
         }
         cout<<"\t<--LAST\n";
     } 
     delete temp;

 }


 int main()
 { 
     char text[] = "c@vijay";

     NODE rear=NULL;  //NODE type variable to point to the rear end of the queue
     NODE front=NULL; //NODE type variable to point to the front end of the queue

     int choice;    //to store the choice entered by user for specific task
     int data_element;  //to store data to be inserted in queue by the user

     while(1)
     {
         system("cls");
         cout<<text<<" \t\t\tQueue PROGRAM\n\nyeah ! time to work on  QUEUE  >>";

         //call to the MF SHOW_DATA
         calling.SHOW_DATA(front);
         cout<<"\n\n\t1.INSERT"
             <<"\n\t2.DELETE"
             <<"\n\t3.EXIT"
             <<"\n\nEnter your choice : ";
         cin>>choice;

         switch( choice ) {

             case 1 : system("cls");
                  cout<<text<<" \t\t\tQueue PROGRAM\n\nINSERTION in queue >"
                  <<"\n\nEnter the value you want to insert : ";
                      cin>>data_element;

                      //invoking MF INSERT_DATA
                      rear=calling.INSERT_DATA( data_element , rear );
                      if(front==NULL)
                          front=rear;
                      break;

                      //invoking MF DELETE_DATA
             case 2 : front=calling.DELETE_DATA(front);
                      break;

             case 3 : exit (0);

             default : system("cls");
                       cout<<text<<" \t\t\tQueue PROGRAM"
                           <<"\n\n\t INVALID choice !!press any key to continue.. > ";
                       getch();

         }

     }

     return 0;

 }

Wednesday, 10 February 2016

Matrix-Multiplication using class(C++)


/* Program that take any two matrices from user as input and show their matrix-multiplication as the result , implemented using class*/

input form of order of matrices from user
input form of matrices elements
output form of final matrix
 #include <iostream>

  using namespace std;
  
  class MATRIX
  {
 
  private :
  int M1[20][20];   //member variable to store matrix 1
  int M2[20][20];   //member variable to store matrix 2
  int M_FINAL[20][20];   //data member to store the final matrix

     public :
  int R1 , C1;
  int R2 , C2;
  int M_MULTI_POSSIBILITY( void );  //M.F 1
  void MATRICES_INPUT( void );      //M.F 2
  void M_MULTIPLICATION( void );    //M.F 3
  void DISPLAY( void );             //M.F 4
 
  };


 //memeber function to check for possibility of matrix- 
 //multiplication
  int MATRIX :: M_MULTI_POSSIBILITY( void )
  {
  int flag = 0;
 
  if( C1 == R2 ) {
  cout<<" Multiplication of given order of matrix is !! POSSIBLE !! ";
      return (++flag);
        }
     
  else {
        cout<<" Multiplication ! NOT POSSIBLE ! with given order of matrix .\n\n";
        return flag;
}
 }
  
  //member function to input-
  //matrices if multiplication condition is -
  //satisfied
  void MATRIX :: MATRICES_INPUT( void )
  {
 
  system("cls");
  int j , k ;
 
       cout<<"Enter elements of matrix 1 >>";
for( j = 0 ; j < R1 ; j++ ) {
          cout<<"\n\n\tRow"<<j+1<<" ->\n";
            for( k = 0 ; k < C1 ; k++ ) {
              cout<<"\tColumn"<<k+1<<" :>\t";
                cin>>M1[j][k];
            } 
       }
        
      cout<<"\nEnter elements of matrix 2 >>";
      for( j = 0 ; j < R2 ; j++ ) {
          cout<<"\n\n\tRow"<<j+1<<" ->\n";
         for( k = 0 ; k < C2 ; k++ ) {
           cout<<"\tColumn"<<k+1<<" :>\t";
             cin>>M2[j][k];
         }
     }
  
 }
  
  //member function to perform multiplication
  void MATRIX :: M_MULTIPLICATION( void )
  {
  int i , j , k ;
   for( i = 0 ; i < R1 ; i++ )
       for( j = 0 ; j < C2 ; j++ )
M_FINAL[i][j]=0;

  for( i = 0 ; i < R1 ; i++ )
     for( j = 0 ; j < C2 ; j++ )
         for( k = 0 ; k < C1 ; k++ )
          M_FINAL[i][j] += M1[i][k] * M2[k][j] ;
     
  }
  
  
  //member function to display final matrix
  void MATRIX :: DISPLAY( void )
  {
 
  system("cls");
       int i , j ;
 
  cout<<"c@vijay\n\n\a\a\tFinal matrix after multiplication is :\n\n\t";
       for( j = 0 ; j < C2 ; j++ )
           cout<<"\tC"<<j+1;
      for( i = 0 ; i < R1 ; i++ ) {
           cout<<"\n\tR"<<i+1<<"\t";
         for( j = 0 ; j < C2 ; j++ )
              cout<<M_FINAL[i][j]<<"\t";
    
         cout<<"\n\t\t";
      }
 
  }
  
  int main( void )
  {
 
  MATRIX obj;  //object creation of MATRIX class



 
       int choice = 0;
 
  do {
 
  system("cls");
 
     cout<<"c@vijay\t\t\tMATRIX MULTIPLICATION ";
  cout<<"\n\nEnter the order of matrix 1 :\n\t no. of rows > ";
  cin>>obj.R1;
  cout<<"\n\t no. of columns > ";
  cin>>obj.C1;
  cout<<"\n\nEnter the order of matrix 2 :\n\t no. of rows > ";
  cin>>obj.R2;
  cout<<"\n\t no. of columns > ";
  cin>>obj.C2;
 
  system("cls");
  if(obj.M_MULTI_POSSIBILITY()) //calling M.F 1
           obj.MATRICES_INPUT();     //calling M.F 2
     
  else
      goto menu;
 
          obj.M_MULTIPLICATION();   //calling M.F 3
      obj.DISPLAY();    // calling M.F 4
 
  menu :
  cout<<"\n\n XD Do you want to enter values again for another matrices \n\n\tPRESS 1 to 'continue' 0 to 'abort' > ";
  cin>>choice;
   
      } while( choice == 1 );
     

return 0;

  }