連結リスト


リンクリストブリーフ

  • 線形収集データ構造
  • ノードのチェーン⛓ - "各ノードは、チェーンの次のノードへの値とポインタを含んでいます」
  • Singly Linked List (one direction only)
    
    1 -> 2 -> 3 -> 4 -> null (when the pointer gets to null, it has reached the end of the linked list) 
    ^
    Head: beginning of the linked list 
    
    //Linked List Node Class Declaration
    class ListNode { 
       constructor(value = 0, next = null) { 
          this.value = value; 
          this.next = next; 
       }
    }
    
    このリンクされたリストシリーズではcurr (現在の場合は)リンクされたリストを移動するメインポインタとして使用します.
    //Using curr allows us to remember the head to return it at the end of the function
    
    let curr = head; 
    
    1 -> 2 -> 3 -> 4 -> null
    ^
    curr
    
    curr = ListNode { 
              value: 1, 
               next: ListNode { 
                       value: 2, 
                        next: ListNode { 
                                 value: 3, 
                                  next: ListNode {
                                         value: 4, 
                                          next: null
                                       }
                              }
                     }
           }
    curr.value = 1
    
    1 -> 2 -> 3 -> 4 -> null
    ^ ->
    curr.next (the next node based on the current node) 
    
    curr.next = ListNode{2, ListNode{3, ListNode{4}}}
    curr.next.value = 2
    
    リンクリスト内の次のノードに移動するには?
    //assigning curr to the next node 
    curr = curr.next; 
    
    以下に例を示します.
    while(curr) { //keep iterating as long as curr is not null
       curr = curr.next; 
    }
    
    While 'curr' is not null: 
    
    1 -> 2 -> 3 -> 4 -> null
    ^ ->
    curr.value = 1
    curr.next.value = 2
    
    curr = curr.next;
    
    __
    
    While 'curr' is not null: 
    
    1 -> 2 -> 3 -> 4 -> null
         ^ ->
    curr.value = 2
    curr.next.value = 3
    
    curr = curr.next;
    
    __
    
    While 'curr' is not null: 
    
    1 -> 2 -> 3 -> 4 -> null
              ^ ->
    curr.value = 3
    curr.next.value = 4
    
    curr = curr.next;
    
    __
    
    While 'curr' is not null: 
    
    1 -> 2 -> 3 -> 4 -> null
                   ^ ->
    curr.value = 4
    curr.next = null
    
    curr = curr.next; 
    
    __
    
    1 -> 2 -> 3 -> 4 -> null
                         ^ ->
    'curr' is null, stop the iteration. 
    The pointer has now moved through the entire Linked List. 
    
    参照と追加リソース:
  • https://www.educative.io/edpresso/what-is-a-linked-list
  • https://www.geeksforgeeks.org/data-structures/linked-list/
  • https://www.geeksforgeeks.org/applications-of-linked-list-data-structure/
  • クラスとは?https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes