使用链接列表"Stop working"的 c++ 代码中出错

Error in my c++ code using linkedlist "Stop working"

本文关键字:c++ 代码 出错 Stop 链接 列表 working      更新时间:2023-10-16

我的代码在那个测试用例中停止了工作,我认为函数Checktables中的错误,但我不确定,我无法修复错误,请帮助我正确地调试这个代码。

测试用例和错误的图像

这是一个带有主.cpp 的cpp文件

    #include"Header.h"
string Name;
string namess;
customer::customer()
{
    name = "";
    gsize = status = 0;
    next = NULL;
}
customer::customer(string name1, int gsize1, int status1)
{
    name = name1;
    gsize = gsize1;
    status = status1;
    next = NULL;
}

waitinglist::waitinglist()
{
    chairnum =50 ;
    totalcustomers = tables = 0;
    head = tail = NULL;
}

waitinglist::waitinglist(int val) 
{
    chairnum = 50;
    totalcustomers = 0;
    tables = 0;
    head = tail = NULL;
}

void waitinglist::change()
{
    customer*temp ;
    temp = head;
    cout << "enter the name: ";
    cin >> namess;
        while (temp != NULL)
        {
            if (namess == temp->name)
            {
                if (temp->status==2)
                {
                    temp->status=1;
                    cout << "done!  " << endl ;
                    break ;
                }
            }
            else if (namess != temp->name)
            {
                temp = temp->next;
            }
        }
      if (temp == NULL)
    {
        cout << "can't found! " << endl;
    }
}

void waitinglist::newcustomer()
{
    customer*tmp = new customer;
    cout << "enter the name: ";  cin >> tmp->name;
    customer*tmpo=new customer;
    tmpo=head ;
    while (tmpo != NULL)
        {
            if (tmp->name != tmpo->name)
            {
                tmpo = tmpo->next;
            }
            else if (tmp->name == tmpo->name)
            {
                cout<<"The Name already exist! " << endl ;
                cout << "enter the name: ";  cin >> tmp->name;
                tmpo=head;
            }
    }
    cout << "enter the group number: ";  cin >> tmp->gsize;
    cout << "enter the status: ";  cin >> tmp->status;
    if (head == NULL)            // linkedlist is empty
    {
        head = tail = tmp;
        totalcustomers++;
    }
    else
    {
        tail->next = tmp;
        tail=tail->next;
        totalcustomers++;
    }
}
void waitinglist::checktables() 
{
        float c=5.00;
        customer*temp=head;
        customer*found;
        cout<<"enter number of tables: ";
        cin >> tables ;
        while (tables>=1 && temp!=NULL)
        {
            int x;
            float y;

            y=((temp->gsize)/c);
            x=(temp->gsize)/c;
            if (tables<y)
            {
                temp=temp->next;
            }
            else if (tables>=y)
            {
                if (x==y)
                {
                    tables=tables-x ;           //   Correct Table!
                    cout<<temp->name<<endl;

                }
                else if (x!=y)
                {
                    tables=tables-(x+1);
                    cout<<temp->name<<endl;
                }
                found=temp ;
                delete found;               // Discard  
                break ;
            }
        }
    }
void waitinglist::displayall()
{
    customer *tmp;
    tmp = head;
    if (tmp == NULL)
    {
        cout << "Empty!";
    }
    while (tmp != NULL)
    {
        cout << "Name: " << tmp->name <<endl; 
        cout << "group number: " << tmp->gsize << endl;
        tmp = tmp->next;
    }
    cout << endl;
}
void waitinglist::diplaycustomer()
{
    customer*tmp;
    tmp = head;
    cout << "enter the name: ";
    cin >> Name;
        while (tmp != NULL)
        {
            if (Name == tmp->name)
            {
                cout << "the name : " << tmp->name << endl;
                cout << "the group size = " << tmp->gsize << endl;
                cout << "the status = " << tmp->status << endl;
                break;
            }
            else if (Name != tmp->name)
            {
                tmp = tmp->next;
            }
        }
      if (tmp == NULL)
    {
        cout << "can't found!" << endl;
    }
}

int main()
{
    int choice;
    string name1 = "";
    int gsize1 = 0;
    int status1 = 0;
    waitinglist mylist;
    cout << "Note: 1 in status means the customer not here and 2 means the customer is here.n";
    cout << "Select your option.nn";
    cout << "(1) Add a new Customer.n";
    cout << "(2) Display information based on Name.n";
    cout << "(3) List all Names.n";
    cout << "(4) to change the status. n" ;
    cout << "(5) Check tables by name. n";
    cout << "(6) quit. n";
    do
    {
        cout << "n";
        cout << "Enter your choice: -->  ";
        cin >> choice;
        if (1 <= choice && choice <= 5)
        {
            switch (choice)
            {
            case 1:
                mylist.newcustomer();
                break;
            case 2:
                mylist.diplaycustomer();
                break;
            case 3:
                mylist.displayall();
                break;
            case 4:
                mylist.change() ;
                break;
            case 5 :
                mylist.checktables();
                break;
            default:
                cout << "Invalid choice.  Enter again.nn";
                break;
            }
        }
        else if (choice>6)
        {
            cout << "Invalid choice.  Enter again.nn";
            break;
        }
    } while (choice != 6);
    return 0;
}

这是头文件.h

        #include<iostream>
    #include<string>
    using namespace std;
    class customer
    {
    public:
        string name;
        int gsize;
        int status;
        customer* next;
        customer();
        customer(string,int,int);
    };
    class waitinglist
    {
    public:
        int tables; //number of occupied tables
        int chairnum;
        int totalcustomers;
        customer*head,*tail;
        waitinglist();
        waitinglist(int);
        void newcustomer();
        void diplaycustomer();
        void displayall();
        void change () ;
        void checktables();
    };

一个错误是,checktables函数通过调用其中一个节点上的delete来破坏链表结构:

        found = temp;
        delete found;               // Discard  

在上面的这些行中,您刚刚做的是创建一个包含断开(无效)链接的链表。任何现在遍历该列表的函数(如displaytables)现在都会碰到断开的链接,事情开始失控。

要从链表中删除节点,您不仅需要调用delete,还需要调整waitinglist中用于指向该已删除节点的链接,并使其指向已删除节点之后的下一个节点。

把它想象成一个真正的链条——如果链条中的一个链环需要移除,你必须从物理上移除它,并在它连接到下一个好链环之前将其钩住。你没有做这一步。

我不会为此编写代码,但这是您在开发程序的早期就应该看到的。更好的方法是编写一个单独的链表类,首先正确地添加和删除节点。测试它,一旦它可以成功添加和删除节点而不会出错,就可以在更大的程序中使用它。