0

又是我。我的搜索程序快完成了。但是,在单链表中搜索时会出现运行时错误。只有当我使用范围搜索时才会发生这种情况。感谢您的帮助。这是相关的代码。

int casearchrange(LIST *list,STUDENT **pPre,STUDENT **pLoc,int upper,int lower){
    *pLoc = list->head;
    for (;*pLoc!=NULL && lower>(*pLoc)->ca;){
        *pPre = *pLoc;
        *pLoc = (*pLoc)->next;
    }
    if (*pLoc==NULL)
        return 0;
    else {
        for (;(*pLoc)->ca<upper&&*pLoc!=NULL;)
            if ((*pLoc)->ca>=lower&&(*pLoc)->ca<=upper){
                printf("%s\n%d  | %-18s|  %0.1f  |  %0.1f",RESULT,(*pLoc)->sid,(*pLoc)->name,(*pLoc)->ca,(*pLoc)->exam);
                *pLoc=(*pLoc)->next;
            }
            fflush(stdin);getch();
            return 1;
        }
}
4

1 回答 1

3

The tests in the line

for (;(*pLoc)->ca<upper&&*pLoc!=NULL;)

are the wrong way round. You'll dereference *pLoc as part of the (*pLoc)->ca<upper test before checking for *pLoc!=NULL. The fix is simply to swap the order of the tests

for (;*pLoc!=NULL && (*pLoc)->ca<upper;)
于 2013-05-07T07:58:15.580 回答