C/C++編程筆記:連接列表(鏈表)丨刪除節點的操做源碼

咱們已經在之前關於單連接列表的文章中討論了「連接列表介紹」和「連接列表插入」。node

讓咱們制定問題陳述以瞭解刪除過程。給定一個「鍵」,刪除該鍵在鏈表中的第一個匹配項。 c++

要從連接列表中刪除節點,咱們須要執行如下步驟。 編程

1)找到要刪除的節點的上一個節點。 微信

2)更改上一個節點的下一個節點。 學習

3)待刪除節點的可用內存。spa

因爲鏈表的每一個節點都是使用C語言中的malloc()動態分配的,所以咱們須要調用free()來釋放爲要刪除的節點分配的內存。3d

C ++code

    #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語言視頻

    #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;

    }

 

輸出: blog

建立的連接列表:  2 3 1 7

刪除後的連接列表:  2 3 7

但願對你有幫助~

另外若是你想更好的提高你的編程能力,學好C語言C++編程!彎道超車,快人一步!筆者這裏或許能夠幫到你~

C語言C++編程學習交流圈子,QQ羣1090842465點擊進入】微信公衆號:C語言編程學習基地

分享(源碼、項目實戰視頻、項目筆記,基礎入門教程)

歡迎轉行和學習編程的夥伴,利用更多的資料學習成長比本身琢磨更快哦!

編程學習書籍分享:

編程學習視頻分享:

相關文章
相關標籤/搜索