我在C中,需要在鏈表中刪除多個「key」字符並返回鏈表的頭部。從C中的鏈表中刪除多個'key'節點
如果'密鑰'不是鏈接列表的第一個或最後一個節點'char', ,此功能才能正常工作。示例...使用密鑰「a」
fails: a->d->a->m->NULL (throws error)
fails: t->a->d->a->NULL (throws error)
passes: d->a->g->n->a->b->NULL (returns d->g->n->b->NULL)
此外,任何帶有「密鑰」的重複立即失敗。示例...使用鍵'a'
fails: d->a->a->a->a->r->n->NULL (returns d->a->a->r->n->NULL)
----------------------------- delete()---- -----------------------------------
node* delete2(char key, node* head)
{
/*IF NULL*/
if(!head)
{
return head;
}
node* prev = NULL;
node* current = head;
/*if first node(head) is to be deleted*/
while (current && current->data == key)
{
prev = current;
current = current->next;
head = current;
free(prev);
}
/*scan list left to right*/
while (current)
{
if (current->data == key)
{
prev->next = current->next;
free(current);
current = prev->next;
}
prev = current;
current = current->next;
}
return head;
}
這本單或雙鏈表? –
該列表是單獨的 – user2755244
@ user2755244我編輯了我的答案多次,因爲我犯了兩個錯誤,一個接一個,但現在沒關係。 – Biduleohm