使用 LinkedList 检查值

Check for a value using LinkedList

void List::IsinList(int resnum){
    Node* temp = head;

while (resnum != temp->_number && temp != NULL){ 
    temp = temp->next;
}

if (resnum == temp->_number)
    cout << resnum << " is reserved for " << temp->_name << endl;

if (temp == NULL){
    cout << "Information not found" << endl;
    exit;
}}

最近在做单向链表的练习。如果 "resnum"(预订编号)在列表中,则上述代码有效,但如果我输入的数字不在列表中,则会出现错误:

"AirLine Reservation.exe" has stopped working..."

有人可以帮我解决这个错误吗?

由于 while 循环中的条件,程序崩溃。

您必须检查是否 temp != NULL 您执行 resnum != temp->_number 之前,因为循环按该顺序测试条件并因此尝试访问值NULL 和崩溃。