使用链接列表“停止工作”我的c ++代码出错

时间:2015-12-29 02:04:40

标签: c++ data-structures linked-list

我的代码停止在该测试用例中工作,我认为功能Checktables中的错误,但我不确定,我无法修复错误,请帮我正确调试此代码。

image of a test case and the error

这是一个带有主.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;
}

这是头文件.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();

    };

1 个答案:

答案 0 :(得分:2)

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

        found = temp;
        delete found;               // Discard  

您刚刚在上面这些行中所做的是拥有一个链接列表,其中包含一个损坏的(无效)链接。现在遍历列表的任何函数(如displaytables)现在都会破坏链接,事情开始变得混乱。

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

将其视为真正的链 - 如果需要删除链中的某个链接,则必须将其物理删除,并将链接挂钩到下一个良好链接。你没有做这一步。

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