How to delete a node in a linked list?

后端 未结 3 1461
孤独总比滥情好
孤独总比滥情好 2021-01-23 14:33

This is what I have so far, but it\'s not working in the test file. Basically skips to the else if(cnode == preposition)

void LinkedList::Delete(Nod         


        
3条回答
  •  温柔的废话
    2021-01-23 15:24

    Here are the full code

        class SportShoe  {
        private:
            struct nodeSport {
                int ShoeID;
                char BrandShoe[SIZE]; 
                char TypeShoe[SIZE];
                char ColourShoe[SIZE];
                int SizeShoe;
                float PriceShoe; 
                nodeSport *last;
                };
                nodeSport *first = NULL; 
    
        public:
            int MenuSportShoe();
            void AddSportShoe();
            void DisplaySportShoe();
            void DeleteSportShoe();
            static void ExitSportShoe();
        };
    
       int SportShoe::MenuSportShoe() {
         int OptionSportShoe = 0;
    
        cout << endl;
        cout << "Please select from the menu:" << endl;
        cout << ":: 1 :: Add item to shoe list" << endl;
        cout << ":: 2 :: Display shoes list" << endl;
        cout << ":: 3 :: Delete item from the list" << endl;
        cout << ":: 4 :: Back" << endl;
        cout << "=>> ";
        cin >> OptionSportShoe;
    
        while (OptionSportShoe == 1){
            AddSportShoe();
        }
    
        while (OptionSportShoe == 2){
            DisplaySportShoe();
          }
    
        while (OptionSportShoe == 3){
            DeleteSportShoe();
        }
    
        while (OptionSportShoe == 4){
            ExitSportShoe();
        }
    
        return 0;
      }
    
    void SportShoe::AddSportShoe() {    
        nodeSport *tempShoe1, *tempShoe2; 
    
        tempShoe1 = new nodeSport;
        cout << "Please enter the Shoe ID : (eg. 43210) " << endl;
        cout << "=>> ";
        cin >> tempShoe1->ShoeID;
    
        cout << "Please enter the Shoe Brand: (eg. Adidas) " << endl;
        cout << "=>> ";
        cin.sync();
        cin.getline(tempShoe1->BrandShoe,SIZE);
    
        cout << "Please enter the Shoe Type : (eg. Running) " << endl;
        cout << "=>> ";
        cin.sync();
        cin.getline(tempShoe1->TypeShoe,SIZE);
    
        cout << "What is the Shoe Colour : (eg. Grey) " << endl;
        cout << "=>> ";
        cin.sync();
        cin.getline(tempShoe1->ColourShoe,SIZE);
    
        cout << "Please enter Shoe Size : (eg. 9) " << endl;
        cout << "=>> ";
        cin >> tempShoe1->SizeShoe; 
    
        cout << "Please enter the price of the Shoe : (eg. RM123.45) " << endl;
        cout << "=>> RM ";
        cin >> tempShoe1->PriceShoe;
    
    
        tempShoe1->last = NULL;  
    
    
        if (first == NULL)  
            first = tempShoe1;
        else  
        {
            tempShoe2 = first; 
            while (tempShoe2->last != NULL) 
                tempShoe2 = tempShoe2->last;
    
            tempShoe2->last = tempShoe1;
        }
    
        system("PAUSE");
        MenuSportShoe();
      }
    
    void SportShoe::DisplaySportShoe() {
        nodeSport *tempShoe1;
        tempShoe1 = first;
    
        while(tempShoe1){
            cout << "ID : " << tempShoe1->ShoeID << endl;
            cout << "Brand : " << tempShoe1->BrandShoe << endl;
            cout << "Type : " << tempShoe1->TypeShoe << endl;
            cout << "Colour : " << tempShoe1->ColourShoe << endl;
            cout << "Size : " << tempShoe1->SizeShoe << endl;
            cout << "Price : " << tempShoe1->PriceShoe << endl;
            cout << endl;
            tempShoe1 = tempShoe1->last;
        }
    
        system("PAUSE");
        MenuSportShoe();
      }
    
     void SportShoe::DeleteSportShoe(){
        nodeSport *tempShoe1, *tempShoe2; 
        int DataShoe;
        tempShoe2 = tempShoe1 = first;
    
        if(tempShoe1 == NULL)
        {
            cout << "\nList is empty!" << endl;
            system("PAUSE");
            MenuSportShoe();
        }
    
        while(tempShoe1 != NULL)
        {
            cout << "\nEnter the Shoes ID to be deleted: (eg. 123) ";
            cin >> DataShoe;
    
            tempShoe2 = tempShoe1;
            tempShoe1 = tempShoe1->last;
    
            if(DataShoe == tempShoe1-> ShoeID){ 
                if(tempShoe1 == first)  {
                    first = first->last;
                    cout << "\nData deleted ";
                }
    
                else{
                    tempShoe2->last = tempShoe1->last;
                    if(tempShoe1->last == NULL){
                        tempShoe2 = tempShoe2;
                    }
                    cout << "\nData deleted ";
                }
    
                delete(tempShoe1);
    
                system("PAUSE");
                MenuSportShoe();
            }
    
            else{
                cout << "\nRecord not Found!!!" << endl;
                system("PAUSE");
                MenuSportShoe();
            }
        }
      }
    
      void SportShoe::ExitSportShoe(){
        int sepatu;
    
        cout << endl;
        cout << "Please choose the option below."<> ";
        cin >> sepatu;
    
        while(sepatu == 1){
            SportShoe listShoe;
            listShoe.MenuSportShoe();
        }
    
        while(sepatu == 2){
            HighHeel listShoe;
            listShoe.MenuHighHeel();
        }
    
        while(sepatu == 3){
            cout << "Thank you. Till we meet again."<< endl;
            exit(1);
        }
    
      }
    
      main() {
    
        cout << "Hello! Welcome to MySepatu Online Shop administrator."<< endl;
        cout << endl;
    
        SportShoe::ExitSportShoe();
        HighHeel::ExitHighHeel();
    
        return 0;
      }
    

提交回复
热议问题