1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to Replacing existing element in a node ?

Discussion in 'C++' started by ccspokai, Apr 10, 2011.

  1. ccspokai

    ccspokai New Member

    Joined:
    Apr 10, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Currently this is my assignment question .
    I having problem
    at "modify_data" function which do not replace new input_message
    but remain the old one .
    Please help me with this , thank you


    Code:
    #include <iostream>
    #include <string>
     #include<conio.h>
    using namespace std;
    
    
    
    class Stack    //declaration of class
    {
          struct Input_Node
          {
                 string Input_Message;
                 int Receiver_Num;
                 int Sender_Num;
                 int Priority;
                 int size;
                 Input_Node *next;
                 Input_Node *item;
                
                
                 
                   
                 }
          *New_Input_Node , *Head , *Tail , *Ptr , *Temp , *Previous , *Current ;
     
               
    public:
           
           Stack()
           {
                 Head = NULL;
                
                 }
          
    
    
            
          
           void add_data(string Input_Message,int Receiver_Num, int Sender_Num,int Priority);
           void delete_data(string Input_Message,int Receiver_Num, int Sender_Num,int Priority);
           void modify_data(string Input_Message,int Receiver_Num, int Sender_Num,int Priority);
           void display_data( );
           void load_data();
           void sort_data();
          
         };
    
    
    
    
    void Stack :: display_data()
    {
         Temp = Head;
         while(Temp != NULL){
           cout <<"Message : "<< Temp->Input_Message<<endl;
           cout <<"Sender: "<< Temp->Sender_Num<<endl;
           cout <<"Receiver : "<< Temp->Receiver_Num<<endl;
           cout <<"Priority :"<< Temp->Priority<<endl;
           Temp = Temp -> next;
           }
           delete Temp;
    
    }
    
    //push data
    void Stack :: add_data( string Input_Message,int Receiver_Num, int Sender_Num,int Priority)
    {
         
         
         New_Input_Node =new Input_Node;
         New_Input_Node->Input_Message = Input_Message;
         New_Input_Node->Receiver_Num  = Receiver_Num ;
         New_Input_Node->Sender_Num    = Sender_Num   ;
         New_Input_Node->Priority      = Priority     ;
          
          New_Input_Node ->next=Head;
          Head=New_Input_Node;
             
           }
    
    void Stack :: delete_data ( string Input_Message,int Receiver_Num, int Sender_Num,int Priority)
    {
        int position , i ;
        
        cin >> position;
        
        if ( position == 1)
        {
                    Ptr = Head ;
                    Ptr->Input_Message = Input_Message;
                    Ptr->Receiver_Num  = Receiver_Num ;
                    Ptr->Sender_Num    = Sender_Num   ;
                    Ptr->Priority      = Priority     ;
                   
                    Head=Head->next;
                    delete Ptr;
                        }
        
        
        
        else
        {
            Previous = Head;
            int i = 1;
            while (i<position)
            { 
                  
                Current = Previous ->next;
                Previous->next = Current->next;
                delete Current;
                i++;
                }   
    }
         
    }     
    
    
    void Stack :: modify_data(string Input_Message,int Receiver_Num, int Sender_Num,int Priority)
    {
      
        int position , i ;
        
        cout<<"Please enter position : "<<endl;    
        cin >> position; 
         
         
         if(position == 1)
         {
             int number ;
             cout<<"Please Enter Which To be Modify :"<<endl;
             cout<<"1. Message "<<endl;
             cout<<"2. Receiver Number"<<endl;
             cout<<"3. Sender Number"<<endl;
             cout<<"4. Priority "<<endl;
             cin>>number;
             
             if(number == 1)
               {  
                     
                       i=1;
                       New_Input_Node = Head;
                       cout<<"Please enter new message: "<<endl;                  
                       cin>>Input_Message;
                       while(i<position)
                       {
                                        
                       
                       New_Input_Node =new Input_Node;
                       New_Input_Node->Input_Message = Input_Message;
                       New_Input_Node =  New_Input_Node ->next;
                       
                       i++;
                                                               }
                                                               }                                          
             else
                 cout<<"Invalid number , please enter again : "<<endl;
                 
                 }
           else      
                 cout<<"Invalid position , please enter again : "<<endl;
         
         }
         
         
         
    
    
    
    int main()
              {
                 Stack c1;
                 
                 int choice;
                 char option;
                 string Input_Message;
                 int Receiver_Num;
                 int Sender_Num;
                 int Priority;
                 do
                 {
                    system("cls");
                    cout<<"----------Menu------------"<<endl;
                    cout<<"1.Addition "<<endl;
                    cout<<"2.Deletion "<<endl;
                    cout<<"3.Modification "<<endl;
                    cout<<"Enter Your choice  ?"<<endl;
                    cin>>choice;
                    switch(choice)
                    {
                         case 1 :   
                              cout<<"Enter Message: "<<endl;
                              cin>>Input_Message;
                              cout<<"Enter Receiver Number :"<<endl;
                              cin>>Receiver_Num;
                              cout<<"Enter Sender Number :"<<endl;
                              cin>>Sender_Num;
                              cout<<"Enter Priority - 1 for high and 0 for low : "<<endl;
                              cin>>Priority;
                              c1.add_data(Input_Message , Receiver_Num , Sender_Num , Priority);
                              cout<<"Message has been added !"<<endl;
                              break;
                              
                           case 2:
                              
                              c1.display_data();
                              cout<<"Please enter position to be deleted : "<<endl;
                              c1.delete_data(Input_Message,Receiver_Num, Sender_Num,Priority);
                              c1.display_data(); 
                                    break;   
                           case 3:
                               c1.display_data();
                               c1.modify_data(Input_Message , Receiver_Num , Sender_Num , Priority);
                               c1.display_data();
                               
                               break;
                              
                          
                    }
                 
                    cout<<"Do you want to continue <Y/N> ?";
                    cin>>option;
                 }while(option=='y' || option=='Y');
                getch();
              }
     

Share This Page