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

时间:2019-03-03 19:06:39

标签: c++

我在这里有一些代码。我在这段代码中使用链表。我们可以添加,显示或删除注释。当我想删除某些东西时,会发生问题。 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;
}

1 个答案:

答案 0 :(得分:1)

此代码块,位于您的SportShoe::DeleteSportShoe函数中

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

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

可能应该是:

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

一些改进代码的建议:

  1. 您的HighHeel和SportShoe类别相同,为99%。它们应该有一个通用的基类-尤其是对于链表管理。

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

  3. 不要使用char BrandShoe[SIZE]在C ++中存储字符串。只要输入超过SIZE(10)个字符,它就会中断。使用std::string类。您可以通过#include <string>来获得它-不要与#include <string.h>

  4. 混淆