C++ 我的c+中有错误+;使用linkedlist的代码;“停止工作”;

C++ 我的c+中有错误+;使用linkedlist的代码;“停止工作”;,c++,data-structures,linked-list,C++,Data Structures,Linked List,我的代码在那个测试用例中停止工作了,我认为函数检查表中的错误是错误的,但我不确定,我无法修复错误。请帮助我正确地处理这段代码 这是一个带有main.cpp的cpp文件 #include"Header.h" string Name; string namess; customer::customer() { name = ""; gsize = status = 0; next = NULL; } customer::customer(string name1

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

这是一个带有main.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.\n\n";
    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.\n\n";
                break;
            }
        }
        else if (choice>6)
        {
            cout << "Invalid choice.  Enter again.\n\n";
            break;
        }

    } while (choice != 6);

    return 0;
}
#包括“Header.h”
字符串名;
字符串名称;
客户::客户()
{
name=“”;
gsize=status=0;
next=NULL;
}
customer::customer(字符串名称1,int-gsize1,int-status1)
{
name=name1;
gsize=gsize1;
状态=状态1;
next=NULL;
}
waitinglist::waitinglist()
{
chairnum=50;
totalcustomers=tables=0;
头=尾=空;
}
waitinglist::waitinglist(int val)
{
chairnum=50;
客户总数=0;
表=0;
头=尾=空;
}
void waitinglist::change()
{
客户*临时工;
温度=水头;
名称;
while(temp!=NULL)
{
如果(名称==临时->名称)
{
如果(临时->状态==2)
{
温度->状态=1;
下一步;
}
}
if(temp==NULL)
{
姓名;
客户*tmpo=新客户;
tmpo=头部;
while(tmpo!=NULL)
{
如果(tmp->name!=tmpo->name)
{
tmpo=tmpo->next;
}
else if(tmp->name==tmpo->name)
{
couttmp->gsize;
cout>tmp->status;
如果(head==NULL)//linkedlist为空
{
头=尾=tmp;
总客户++;
}
其他的
{
tail->next=tmp;
tail=tail->next;
总客户++;
}
}
void waitinglist::checktables()
{
浮点数c=5.00;
客户*温度=人头;
找到客户*;
床头柜;
while(表>=1&&temp!=NULL)
{
int x;
浮动y;
y=((温度->gsize)/c);
x=(温度->gsize)/c;
如果(下一步);
}
else if(表>=y)
{
如果(x==y)
{
tables=tables-x;//正确的表格!

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

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

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

把它想象成一条真正的链——如果链中的一个链接需要被移除,你必须从物理上移除它,然后将之前的链接挂到下一个好的链接上。你没有完成这一步


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

因此,请从图像中的对话框中选择“调试”并开始调试。您在错误的网站上。这不是发布整个程序的网站,唯一的描述是“我的代码在该测试用例中停止工作”,甚至没有解释“该测试用例”是什么首先,希望每个人都明白为什么“我的代码在那个测试用例中停止工作”。此代码中的大部分似乎与问题无关。对于任何涉及崩溃的问题,都需要一个和堆栈跟踪。这是我第一次使用此代码site@AhmedAlaa为什么你写了所有这些代码,然后在写了所有这些代码后发现最简单的东西都不起作用?你应该开发代码并单独测试每一个部分。例如,忘记那些花哨的输入——你能插入一个名称并显示插入的名称而不出错吗?写了所有这些代码,却不知道如何修复它——真的没有一个好的借口可以这样做。
        found = temp;
        delete found;               // Discard