數據結構:單向鏈表系列2--插入節點


插入節點

在鏈表中插入節點有以下三種情形:

1、在鏈表頭部

2、在特定節點

3、在鏈表尾部

1)在頭部添加(4步操作)

新節點添加到頭部,將成為新的頭節點,以下將節點添加到鏈表頭部的函數是push,push接收指向鏈表頭部的指針,然后將指針修改

指向新的節點:

c語言:

/* Given a reference (pointer to pointer) to the head of a list 
   and an int,  inserts a new node on the front of the list. */
void push(struct Node** head_ref, int new_data) 
{ 
    /* 1. allocate node */
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); /* 2. put in the data */ new_node->data = new_data; /* 3. Make next of new node as head */ new_node->next = (*head_ref); /* 4. move the head to point to the new node */ (*head_ref) = new_node; } 

Java:

/* This function is in LinkedList class. Inserts a 
   new Node at front of the list. This method is  
   defined inside LinkedList class shown above */
public void push(int new_data) 
{ 
    /* 1 & 2: Allocate the Node & 
              Put in the data*/ Node new_node = new Node(new_data); /* 3. Make next of new Node as head */ new_node.next = head; /* 4. Move the head to point to new Node */ head = new_node; } 

 

push時間復雜度O(1)

2)在特定節點(5步操作)

c語言:

/* Given a node prev_node, insert a new node after the given 
   prev_node */
void insertAfter(struct Node* prev_node, int new_data) 
{ 
    /*1. check if the given prev_node is NULL */ 
    if (prev_node == NULL) { printf("the given previous node cannot be NULL"); return; } /* 2. allocate new node */ struct Node* new_node =(struct Node*) malloc(sizeof(struct Node)); /* 3. put in the data */ new_node->data = new_data; /* 4. Make next of new node as next of prev_node */ new_node->next = prev_node->next; /* 5. move the next of prev_node as new_node */ prev_node->next = new_node; }

java:

/* This function is in LinkedList class. 
   Inserts a new node after the given prev_node. This method is  
   defined inside LinkedList class shown above */
public void insertAfter(Node prev_node, int new_data) 
{ 
    /* 1. Check if the given Node is null */
    if (prev_node == null) { System.out.println("The given previous node cannot be null"); return; } /* 2. Allocate the Node & 3. Put in the data*/ Node new_node = new Node(new_data); /* 4. Make next of new Node as next of prev_node */ new_node.next = prev_node.next; /* 5. make next of prev_node as new_node */ prev_node.next = new_node; } 

因為已經給定了節點,所以insertAfter時間復雜為O(1)

3)在鏈表尾部(6個步驟)

我們需要遍歷鏈表,得到最后一個節點,然后把新節點追加到最后,然后讓他成為新的尾部節點

c語言:

/* Given a reference (pointer to pointer) to the head 
   of a list and an int, appends a new node at the end  */
void append(struct Node** head_ref, int new_data) 
{ 
    /* 1. allocate node */
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); struct Node *last = *head_ref; /* used in step 5*/ /* 2. put in the data */ new_node->data = new_data; /* 3. This new node is going to be the last node, so make next of it as NULL*/ new_node->next = NULL; /* 4. If the Linked List is empty, then make the new node as head */ if (*head_ref == NULL) { *head_ref = new_node; return; } /* 5. Else traverse till the last node */ while (last->next != NULL) last = last->next; /* 6. Change the next of last node */ last->next = new_node; return; } 

java:

/* Appends a new node at the end.  This method is  
   defined inside LinkedList class shown above */
public void append(int new_data) 
{ 
    /* 1. Allocate the Node & 
       2. Put in the data 
       3. Set next as null */ Node new_node = new Node(new_data); /* 4. If the Linked List is empty, then make the new node as head */ if (head == null) { head = new Node(new_data); return; } /* 4. This new node is going to be the last node, so make next of it as null */ new_node.next = null; /* 5. Else traverse till the last node */ Node last = head; while (last.next != null) last = last.next; /* 6. Change the next of last node */ last.next = new_node; return; } 

因為需要從頭到尾循環,所以append時間復雜度為O(n),其中n是節點的個數。

當然你可以修改結構,保存尾節點,使其復雜度降為O(1)

 

來源:https://www.geeksforgeeks.org/linked-list-set-2-inserting-a-node/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM