C/C++程式設計筆記:連結列表(連結串列)丨插入節點的三種方法
在上一篇文章中,我們介紹了連結列表。我們還建立了一個具有3個節點的簡單鏈表,並討論了連結串列遍歷。
本文討論的所有程式均考慮以下連結串列的表示形式。
C ++
C
在這篇文章中,討論了在連結串列中插入新節點的方法。可以通過三種方式新增節點:
1)在連結串列的最前面
2)在給定節點之後。
3)在連結列表的末尾。
在前面新增一個節點:(4個步驟)
將新節點始終新增到給定連結列表的開頭之前。新新增的節點成為連結列表的新頭。例如,如果給定的連結列表為10-> 15-> 20-> 25,並且我們在前面添加了專案5,則連結列表將變為5-> 10-> 15-> 20-> 25。讓我們將新增到列表最前面的函式稱為push()。push()必須接受一個指向頭指標,因為必須推頭指標改為指向新的節點。
以下是在最前面新增節點的4個步驟。
C++
C
push()的時間複雜度為O(1),因為它要做的工作量是恆定的。
在給定節點之後新增一個節點:(5個步驟)
給我們一個指向節點的指標,並將新節點插入到給定節點之後。
C ++
C
insertAfter()的時間複雜度為O(1),因為它的工作量是恆定的。
在最後新增一個節點:(6個步驟)
將新節點始終新增到給定連結列表的最後一個節點之後。例如,如果給定的連結列表為5-> 10-> 15-> 20-> 25,並且我們在末尾添加了專案30,則連結列表將變為5-> 10-> 15-> 20-> 25- > 30。
由於連結列表通常由其頭部表示,因此我們必須遍歷該列表直至結束,然後將最後一個節點的下一個更改為新節點。
以下是最後新增節點的6個步驟。
C
append的時間複雜度為O(n),其中n是連結串列中節點的數量。由於從頭到尾都有一個迴圈,因此該函式可以執行O(n)。
還可以通過保留指向連結串列尾部的額外指標,將該方法優化為在O(1)中工作。
以下是使用上述所有方法建立連結串列的完整程式。
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; } voidinsertAfter(Node* prev_node, intnew_data) { if(prev_node == NULL) { cout<<"the given previous node cannot be NULL"; return; } Node* new_node = newNode(); new_node->data = new_data; new_node->next = prev_node->next; prev_node->next = new_node; } voidappend(Node** head_ref, intnew_data) { Node* new_node = newNode(); Node *last = *head_ref; /* used in step 5*/ new_node->data = new_data; new_node->next = NULL; then make the new node as head */ if(*head_ref == NULL) { *head_ref = new_node; return; } while(last->next != NULL) last = last->next; last->next = new_node; return; } voidprintList(Node *node) { while(node != NULL) { cout<<" "<<node->data; node = node->next; } } int main() { Node* head = NULL; append(&head, 6); push(&head, 7); push(&head, 1); append(&head, 4); insertAfter(head->next, 8); cout<<"Created Linked list is: "; 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; } voidinsertAfter(structNode* prev_node, intnew_data) { /*1. check if the given prev_node is NULL */ if(prev_node == NULL) { printf("the given previous node cannot be NULL"); return; } structNode* new_node =(structNode*) malloc(sizeof(structNode)); new_node->data = new_data; new_node->next = prev_node->next; prev_node->next = new_node; } voidappend(structNode** head_ref, intnew_data) { structNode* new_node = (structNode*) malloc(sizeof(structNode)); structNode *last = *head_ref; /* used in step 5*/ new_node->data = new_data; it as NULL*/ new_node->next = NULL; if(*head_ref == NULL) { *head_ref = new_node; return; } while(last->next != NULL) last = last->next; last->next = new_node; return; } voidprintList(structNode *node) { while(node != NULL) { printf(" %d ", node->data); node = node->next; } } intmain() { structNode* head = NULL; append(&head, 6); push(&head, 7); push(&head, 1); append(&head, 4); insertAfter(head->next, 8); printf("\n Created Linked list is: "); printList(head); return0; }
輸出:
建立的連結列表為:1 7 8 6 4
希望本篇文章對你有幫助~
另外如果你想更好的提升你的程式設計能力,學好C語言C++程式設計!彎道超車,快人一步!筆者這裡或許可以幫到你~
C語言C++程式設計學習交流圈子,QQ群1090842465【點選進入】微信公眾號:C語言程式設計學習基地
分享(原始碼、專案實戰視訊、專案筆記,基礎入門教程)
歡迎轉行和學習程式設計的夥伴,利用更多的資料學習成長比自己琢磨更快哦!
程式設計學習書籍分享:
程式設計學習視訊分享: