【发布时间】:2020-12-08 09:41:26
【问题描述】:
您好,我有一个使用结构的有序链表。该结构有两个元素:字符串名称和 int id。我正在尝试搜索特定节点,以便可以打印它,但是当我尝试运行代码时,我不断收到“核心转储(分段错误)”错误。
/*
* search: Auxiliary function that searches for a specific node in the list.
*/
node* search(node* head, int c){
node* aux = head;
while (aux != NULL && aux->id != c)
aux = aux->next;
return aux;
}
/*
* showNode: Prints the information of a specific node (search is done by ID).
*/
void showNode(node* head, int c) {
node* aux = head;
node* resp;
if (aux != NULL) {
resp = search(aux, c);
if(resp->id == c)
print(resp);
else
printf("Node wasn't found");
} else {
printf("Error: Empty list. \n");
}
}
【问题讨论】:
-
如果
resp == nullptr怎么办? -
if (resp != null && resp->id == c)之类的。和aux->id > c。