国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

C/C++編程筆記:鏈接列表(鏈表)丨刪除節(jié)點(diǎn)的操作源碼

2020-12-11 21:00 作者:C語(yǔ)言編程__Plus  | 我要投稿

我們已經(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í)視頻分享:



C/C++編程筆記:鏈接列表(鏈表)丨刪除節(jié)點(diǎn)的操作源碼的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
台湾省| 山丹县| 平谷区| 建平县| 峨边| 万山特区| 肥东县| 怀集县| 茌平县| 特克斯县| 毕节市| 贵州省| 麟游县| 阿图什市| 澳门| 息烽县| 措美县| 山阳县| 新闻| 高青县| 青川县| 裕民县| 界首市| 青州市| 佛学| 利辛县| 抚顺市| 南昌市| 娄底市| 镇安县| 汶川县| 青川县| 平凉市| 长寿区| 博白县| 郯城县| 宁夏| 吉首市| 武川县| 峨边| 兰溪市|