C/C++編程筆記:鏈接列表(鏈表)丨刪除節(jié)點(diǎn)的操作源碼
我們已經(jīng)在以前關(guān)于單鏈接列表的文章中討論了“鏈接列表介紹”和“鏈接列表插入”。
讓我們制定問(wèn)題陳述以了解刪除過(guò)程。給定一個(gè)“鍵”,刪除該鍵在鏈表中的第一個(gè)匹配項(xiàng)。?
要從鏈接列表中刪除節(jié)點(diǎn),我們需要執(zhí)行以下步驟。?
1)找到要?jiǎng)h除的節(jié)點(diǎn)的上一個(gè)節(jié)點(diǎn)。?
2)更改上一個(gè)節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)。?
3)待刪除節(jié)點(diǎn)的可用內(nèi)存。

由于鏈表的每個(gè)節(jié)點(diǎn)都是使用C語(yǔ)言中的malloc()動(dòng)態(tài)分配的,因此我們需要調(diào)用free()來(lái)釋放為要?jiǎng)h除的節(jié)點(diǎn)分配的內(nèi)存。
C ++
#include <bits/stdc++.h>
usingnamespacestd;
classNode{
public:
????intdata;
????Node* next;
};
voidpush(Node** head_ref, intnew_data)
{
????Node* new_node = newNode();
????new_node->data = new_data;
????new_node->next = (*head_ref);
????(*head_ref) = new_node;
}
voiddeleteNode(Node** head_ref, intkey)
{
????Node* temp = *head_ref;
????Node* prev = NULL;
????if(temp != NULL && temp->data == key)
????{
????????*head_ref = temp->next;?
????????delete temp;? ? ? ? ? ?
????????return;
????}
????while(temp != NULL && temp->data != key)
????{
????????prev = temp;
????????temp = temp->next;
????}
????if(temp == NULL)
????????return;
????prev->next = temp->next;
????delete temp;
}
voidprintList(Node* node)
{
????while(node != NULL)?
????{
????????cout << node->data << " ";
????????node = node->next;
????}
}
intmain()
{
????Node* head = NULL;
????push(&head, 7);
????push(&head, 1);
????push(&head, 3);
????push(&head, 2);
????puts("Created Linked List: ");
????printList(head);
????deleteNode(&head, 1);
????puts("\nLinked List after Deletion of 1: ");
????printList(head);
????return 0;
}
C語(yǔ)言
#include <stdio.h>
#include <stdlib.h>
structNode
{
????intdata;
????structNode *next;
};
voidpush(structNode** head_ref, intnew_data)
{
????structNode* new_node = (structNode*) malloc(sizeof(structNode));
????new_node->data? = new_data;
????new_node->next = (*head_ref);
????(*head_ref)??? = new_node;
}
voiddeleteNode(structNode **head_ref, intkey)
{
????structNode* temp = *head_ref, *prev;
????if(temp != NULL && temp->data == key)
????{
????????*head_ref = temp->next;??
????????free(temp);?
????????return;
????}
????while(temp != NULL && temp->data != key)
????{
????????prev = temp;
????????temp = temp->next;
????}
????if(temp == NULL) return;
????prev->next = temp->next;
????free(temp);??
}
voidprintList(structNode *node)
{
????while(node != NULL)
????{
????????printf(" %d ", node->data);
????????node = node->next;
????}
}
int main()
{
????structNode* head = NULL;
????push(&head, 7);
????push(&head, 1);
????push(&head, 3);
????push(&head, 2);
????puts("Created Linked List: ");
????printList(head);
????deleteNode(&head, 1);
????puts("\nLinked List after Deletion of 1: ");
????printList(head);
????return0;
}
輸出:?
創(chuàng)建的鏈接列表:? 2 3 1 7
刪除后的鏈接列表:? 2 3 7
希望對(duì)你有幫助~
另外如果你想更好的提升你的編程能力,學(xué)好C語(yǔ)言C++編程!彎道超車,快人一步!筆者這里或許可以幫到你~

UP在主頁(yè)上傳了一些學(xué)習(xí)C/C++編程的視頻教程,有興趣或者正在學(xué)習(xí)的小伙伴一定要去看一看哦!會(huì)對(duì)你有幫助的~
分享(源碼、項(xiàng)目實(shí)戰(zhàn)視頻、項(xiàng)目筆記,基礎(chǔ)入門教程)
歡迎轉(zhuǎn)行和學(xué)習(xí)編程的伙伴,利用更多的資料學(xué)習(xí)成長(zhǎng)比自己琢磨更快哦!
編程學(xué)習(xí)書(shū)籍分享:

編程學(xué)習(xí)視頻分享:
