我撰寫了以下用于從串列中洗掉節點的函式:
int remove_node_in_list( Node **set, size_t pos, Node **head )
{
if( *head == NULL ){
printf("LIST IS EMPTY\n");
}
int success = set[pos] != NULL;
if ( success )
{
Node *tmp = set[pos];
set[pos] = set[pos]->next;
free(tmp);
}
return success;
}
但是,我認為檢查我的串列是否為空是行不通的。我只想檢查我的串列是否為空,以便為它列印一條訊息,并且沒有任何內容可以洗掉。
uj5u.com熱心網友回復:
您應該在 if 中放置一個 return 來檢查串列是否為空,如果串列為空,則不應進一步處理該函式。在呼叫該函式時如何處理該“錯誤”取決于您,例如:
int remove_node_in_list( Node **set, size_t pos, Node **head )
{
if( *head == NULL )
return -1;
int success = set[pos] != NULL;
if ( success )
{
Node *tmp = set[pos];
set[pos] = set[pos]->next;
free(tmp);
}
return success;
}
/* Inside main */
response = remove_node_in_list(args);
if (response == -1)
printf("List is empty");
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/441816.html
上一篇:C語言:如何讀取像“SomeWord(Int)”這樣的輸入,沒有空格,只有一個單詞,然后是圓括號內的一個數字
下一篇:檢查檔案是否在C中打開
