搜索单链接列表程序中的记录时出现运行时错误

时间:2017-07-02 10:16:19

标签: c++

我正在编写一个简单的程序,用于存储汽车数据的单个链表(使用Car类),它们的名称(20个字符长),型号(某个整数值)和价格(一些浮点值)的车。但是,当我使用下面的函数搜索列表中的特定记录(条目)时,程序显示错误。我只想通过输入型号来搜索特定记录。为此我有一个START指针指向第一个节点&
当前,前一个遍历列表的指针。该程序正常工作,直到第一个“if”块。一旦进入while循环,弹出对话框就会说program.exe已停止工作。我在程序中找不到导致它崩溃的错误。提前致谢!

void car_search(int m)
{
    if(record_empty())
    {
        cout<<"\nThe list is empty. Enter some records first!\n";
        return;
    }

    else
    {
        Car *previous,*current;
        previous=START;
        current=START;
      while(m>= current->model_no && current!= NULL)
        {
            previous=current;
            current=current->next;
        }
    if(previous->model_no!=m) cout<<"\nRecord not found. Try a different model no.\n";

    else
        {
            cout<<"\nRecord found\n";
            cout<<"Model number is    "<<previous->model_no<<endl;
            cout<<"Name of the car is "<<previous->name<<endl;
            cout<<"Cost of the car is "<<previous->price<<endl;
        }


    }
}

0 个答案:

没有答案