最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(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ó)家法律
麻栗坡县| 安化县| 山阴县| 吴川市| 同江市| 黔西| 彭阳县| 连南| 浦县| 甘南县| 高雄县| 巴中市| 顺昌县| 房产| 孟津县| 红桥区| 仪陇县| 富阳市| 西吉县| 财经| 长海县| 泰来县| 泗水县| 靖远县| 霞浦县| 武山县| 新宾| 天长市| 岚皋县| 商都县| 禹城市| 迁西县| 蚌埠市| 通城县| 班戈县| 镇沅| 舒兰市| 姜堰市| 松溪县| 交城县| 延吉市|