为什么不能删除正确的节点?

Why can't delete the right node?

本文关键字:节点 不能 删除 为什么      更新时间:2023-10-16

我在这里有一些代码。我正在使用此代码中的链接列表。我们可以添加注意,显示并删除。当我想删除某些内容时,就会出现问题。1.创建一个节点,然后尝试删除它。它可以检测和删除节点。2.创建两个节点,然后我尝试删除第一个节点。但是它删除了第二个。我现在真的没有想法。希望任何人都可以帮助我。谢谢你

#include <iostream>
#include <string.h>
using namespace std;
const int SIZE = 10;
//1st class
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();
   };
 //2nd class
class HighHeel  {
    private:
        struct nodeHeel {
            int ProductCode;
            char BrandHeel[SIZE]; 
            char MaterialHeel[SIZE];
            char ColourHeel[SIZE];
            int HeightHeel;
            float PriceHeel; 
            nodeHeel *next; 
        };
    nodeHeel *start = NULL; 
    public:
        int MenuHighHeel();
        void AddHighHeel();
        void DisplayHighHeel();
        void DeleteHighHeel();
        static void ExitHighHeel()
        {
            SportShoe::ExitSportShoe();
        }
 };
int SportShoe::MenuSportShoe() {
    int OptionSportShoe = 0;
    cout << endl;
    cout << ">> Please select from the menu below <<" << 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;
 }
 int HighHeel::MenuHighHeel() {
    int OptionHighHeel = 0;
    cout << endl;
    cout << ">> Please select from the menu below <<" << endl;
    cout << ":: 1 :: Add item to the Heel List" << endl;
    cout << ":: 2 :: Display the Heel List" << endl;
    cout << ":: 3 :: Delete item from the list" << endl;
    cout << ":: 4 :: Back" << endl;
    cout << "=>> ";
    cin >> OptionHighHeel;
    while (OptionHighHeel == 1){
        AddHighHeel();
    }
    while (OptionHighHeel == 2){
        DisplayHighHeel();
    }
    while (OptionHighHeel == 3){
        DeleteHighHeel();
    }
    while (OptionHighHeel == 4){
        SportShoe::ExitSportShoe();
    }
    return 0;
  }
 void SportShoe::AddSportShoe() {   
    nodeSport *tempShoe1, *tempShoe2; 
    tempShoe1 = new nodeSport;
    cout << "Sport Shoe Section." << endl;
    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 HighHeel::AddHighHeel() { 
    nodeHeel *tempHeel1, *tempHeel2; 
    tempHeel1 = new nodeHeel;
    cout << "Heel Section." << endl;
    cout << "Please enter Heel Code : (eg. 98765) " << endl;
    cout << "=>> ";
    cin >> tempHeel1->ProductCode;
    cout << "Please enter Heel Brand: (eg. Gucci) " << endl;
    cout << "=>> ";
    cin.sync();
    cin.getline(tempHeel1->BrandHeel,SIZE);
    cout << "Please enter Heel Material : (eg. Leather) " << endl;
    cout << "=>> ";
    cin.sync();
    cin.getline(tempHeel1->MaterialHeel,SIZE);
    cout << "What is the Heel Colour : (eg. Red) " << endl;
    cout << "=>> ";
    cin.sync();
    cin.getline(tempHeel1->ColourHeel,SIZE);
    cout << "Please enter Heel Height (cm) : (eg. 2.25) " << endl;
    cout << "=>> ";
    cin >> tempHeel1->HeightHeel; 
    cout << "Please enter the Heel Price : (eg. RM123.45) " << endl;
    cout << "=>> RM ";
    cin >> tempHeel1->PriceHeel;
    tempHeel1->next = NULL;  

    if (start == NULL)  
        start = tempHeel1;
    else  
    {
        tempHeel2 = start; 
        while (tempHeel2->next != NULL) 
            tempHeel2 = tempHeel2->next;
        tempHeel2->next = tempHeel1;
    }
    system("PAUSE");
    MenuHighHeel();
}
void SportShoe::DisplaySportShoe() {
    nodeSport *tempShoe1;
    tempShoe1 = first;
    if (tempShoe1 == NULL){
        cout << "List empty." << endl;
        cout << endl;
        system("PAUSE");
        MenuSportShoe();        
    }
    else{
        while(tempShoe1){
            cout << "Sport Shoe Section." << endl;
            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 HighHeel::DisplayHighHeel() {
    nodeHeel *tempHeel1;
    tempHeel1 = start;
    if (tempHeel1 == NULL){
        cout << " List empty." << endl;
        cout << endl;
        system("PAUSE");
        MenuHighHeel();     
    }
    else{
        while(tempHeel1){
            cout << "Heel Section." << endl;
            cout << "Heel Code =>> " << tempHeel1->ProductCode << endl;
            cout << "Brand =>> " << tempHeel1->BrandHeel << endl;
            cout << "Material =>> " << tempHeel1->MaterialHeel << endl;
            cout << "Colour =>> " << tempHeel1->ColourHeel << endl;
            cout << "Height (cm) =>> " << tempHeel1->HeightHeel << endl;
            cout << "Price =>> " << tempHeel1->PriceHeel << endl;
            cout << endl;
            tempHeel1 = tempHeel1->next;
        }
        system("PAUSE");
        MenuHighHeel();
    }
}
void SportShoe::DeleteSportShoe(){
    nodeSport *tempShoe1, *tempShoe2; 
    int DataShoe;
    cout << "Sport Shoe Section." << endl;
    cout << "nEnter the Shoes ID to be deleted: (eg. 123) "<< endl;
    cout << "=>> ";
    cin >> DataShoe;
    tempShoe2 = tempShoe1 = first;
    while((tempShoe1 != NULL) && (DataShoe == tempShoe1-> ShoeID))
    {
        tempShoe2 = tempShoe1;
        tempShoe1 = tempShoe1->last;
    }
    if(tempShoe1 == NULL)
    {
        cout << "nRecord not Found!!!" << endl;
        system("PAUSE");
        MenuSportShoe();
    }
    if((tempShoe1 == first) && (DataShoe == tempShoe1-> ShoeID))
    {
        first = first->last;
        cout << "nData found " << endl;
    }
    else{
        tempShoe2->last = tempShoe1->last;
        if(tempShoe1->last == NULL){
            tempShoe2 = tempShoe2;
        }
    cout << "nData deleted "<< endl;
    }
    delete(tempShoe1);
    cout << endl;
    system("PAUSE");
    MenuSportShoe();
}
void HighHeel::DeleteHighHeel(){
    nodeHeel *tempHeel1, *tempHeel2;  
    int DataHeel;
    cout << "Heel Section." << endl;
    cout << "nEnter the Heel Code to be deleted: (eg. 123) "<< endl;
    cout << "=>> "; 
    cin >> DataHeel;
    tempHeel2 = tempHeel1 = start;
    while((tempHeel1 != NULL) && (DataHeel == tempHeel1->ProductCode))
    {
        tempHeel2 = tempHeel1;
        tempHeel1 = tempHeel1->next;
    }
    if(tempHeel1 == NULL)
    {
        cout << "nRecord not Found!!!" << endl;
        system("PAUSE");
        MenuHighHeel();
    }
    if(tempHeel1 == start)
    {
        start = start->next;
        cout << "nData deleted "<< endl;
    }
    else{
        tempHeel2->next = tempHeel1->next;
        if(tempHeel1->next == NULL){
            tempHeel2 = tempHeel2;
        }
    cout << "nData deleted "<< endl;
    }
    delete(tempHeel1);
    cout << endl;
    system("PAUSE");
    MenuHighHeel();
}
void SportShoe::ExitSportShoe(){
    int sepatu;
    cout << endl;
    cout << ">> Please choose the option below <<"<<endl;
    cout << ":: 1 :: Sport Shoe." << endl;
    cout << ":: 2 :: Ladies High Heel." << endl;
    cout << ":: 3 :: Exit" << endl;
    cout << "=>> ";
    cin >> sepatu;
    while(sepatu == 1){
        SportShoe listShoe;
        listShoe.MenuSportShoe();
    }
    while(sepatu == 2){
        HighHeel listShoe;
        listShoe.MenuHighHeel();
    }
    while(sepatu == 3){
        cout << ">> Have a nice day. See you soon! <<"<< endl;
        exit(1);
    }
}
main() {
    cout << ">> Hello! Welcome to MySepatu Online (Administrator Site) <<"; 
    cout << endl;
    SportShoe::ExitSportShoe();
    return 0;
}

在您的SportShoe::DeleteSportShoe函数中,这个代码块

while ((tempShoe1 != NULL) && (DataShoe == tempShoe1->ShoeID))
{
    tempShoe2 = tempShoe1;
    tempShoe1 = tempShoe1->last;
}

一旦遇到具有匹配ID的列表中的第一个节点,while while循环就会立即停止。这意味着后续代码将删除错误的节点或根本没有。

可能应该是:

while ((tempShoe1 != NULL) && (DataShoe != tempShoe1->ShoeID))
{
    tempShoe2 = tempShoe1;
    tempShoe1 = tempShoe1->last;
}

一些有关改进代码的建议:

  1. 您的高摩尔和Sportshoe类别为99%。他们应该有一个共同的基类 - 尤其是对于链接列表管理。

  2. 完全分开用户界面(菜单打印和输入)代码形式的代码维护您的数据模型(链接列表)。

  3. 不要使用char BrandShoe[SIZE]将字符串存储在C 中。当我输入大小以上(10)个字符时,这立即破裂。使用std::string类。您可以通过#include <string>-不要与#include <string.h>

  4. 混淆