在上一篇文章中,咱們介紹了連接列表。咱們還建立了一個具備3個節點的簡單鏈表,並討論了鏈表遍歷。node
本文討論的全部程序均考慮如下鏈表的表示形式。 c++
C ++編程
C微信
在這篇文章中,討論了在鏈表中插入新節點的方法。能夠經過三種方式添加節點: app
1)在鏈表的最前面 函數
2)在給定節點以後。 學習
3)在連接列表的末尾。優化
在前面添加一個節點:(4個步驟)spa
將新節點始終添加到給定連接列表的開頭以前。新添加的節點成爲連接列表的新頭。例如,若是給定的連接列表爲10-> 15-> 20-> 25,而且咱們在前面添加了項目5,則連接列表將變爲5-> 10-> 15-> 20-> 25。讓咱們將添加到列表最前面的函數稱爲push()。push()必須接受一個指向頭指針,由於必須推頭指針改成指向新的節點。3d
如下是在最前面添加節點的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語言編程學習基地
分享(源碼、項目實戰視頻、項目筆記,基礎入門教程)
歡迎轉行和學習編程的夥伴,利用更多的資料學習成長比本身琢磨更快哦!
編程學習書籍分享:
編程學習視頻分享: