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;

 }

No comments:

Post a Comment