A list

List

List

First, what is the list?

  1. And arrays, is also a linear list table.
  2. From memory structure, linked list memory structure is not continuous memory space, a set of memory blocks is fragmented in series, so that a data structure of data stored.
  3. Each memory block list is referred to as a node Node. In addition to data storage nodes, the need to record a vertical address of the node chain, i.e. subsequent pointer next.

Second, the list of features

  1. Insert, delete data of high efficiency, O (1) level (just change the pointer to it), a random access efficiency low O (n) level (required to traverse the chain to the head end of the chain).
  2. And an array compared to consume more memory space, since each node stores the data need additional space to store a successor pointer.

Third, the usual list: single linked list, circular list and doubly linked list

Single list

enter description here
1) Each node contains only a pointer, i.e. successor pointers.
2) single chain has two special nodes, i.e., the head node and tail node. Why special? Successor list pointer indicates the whole, the tail node to the first node with an empty address null address.
3) Features: time complexity insertion and deletion of nodes is O (1), to find the time complexity is O (n).

Circular list

enter description here

1) are consistent with a single linked list in addition to the tail pointer address of the next node to the first node.

2) apply to circulation data storage features, such as Joseph problems.

  //约瑟夫环运作如下:
  //1、一群人围在一起坐成环状(如:N)
  //2、从某个编号开始报数(如:K)
  //3、数到某个数(如:M)的时候,此人出列,下一个人重新报数
  //4、一直循环,直到所有人出列[3]  ,约瑟夫环结束
  public class yuesefu 
  {
      static class Node
      {
          int val;
          Node next;
          Node(int v)
          {
              val=v;
          }        
      }
      public static void main(String[] args) {
          int N=9;//表示总个数
          int M=5;//数到几出列
          
          //头节点单列出来,方便形成循环链表
          Node t=new Node(1);
          Node x=t;
          
          //建立单向链表
          for(int i=2;i<=N;i++)
          {
              x=(x.next=new Node(i));
          }    
          
          //最后一个节点的next指向第一个节点,形成循环链表
          x.next=t;
          System.out.println("依次出来的顺序为:");
          while(x!=x.next)
          {
              for(int i=1;i<M;i++)
              {
                  x=x.next;
              }    
              System.out.print(x.next.val+" ");
              x.next=x.next.next;
          }
          System.out.println();
          System.out.println("最后剩余的是: "+x.val);    
      }
  }

Doubly linked list

enter description here
1) In addition to storing node data, there are two pointers point to the previous node address (pointer precursor prev) and the next node address (successor pointer next).
2) Subsequent pointer precursor prev pointer and a tail node of the first node point to null address.
3) Features:
single-chain compared to the same data store, need to consume more storage space.
Insert, delete operations more efficient than a single linked list O (1) level. Operation to remove, for example, a delete operation is divided into two cases: a given data value and deletes the corresponding node address of the node to delete the given node.
In the former case , single chain and two-way linked list need to traverse from beginning to end to find the corresponding node to delete, the time complexity is O (n).
For the second case , the operation must be performed to find the predecessor node to delete, from start to finish a single traverse the linked list until p-> next = q, the time complexity is O (n), and the doubly linked list can be found immediate predecessor node, time complexity is O (1).
For an ordered list, doubly linked list by value queries more efficient than single-chain higher. Because we can find the last recorded position p, every query, depending on the size relationship you are looking for value and p, the decision to move forward or backward to find, so look for an average of only half of the data.

  • Bidirectional circular list: pointer to the first node to the predecessor node of the tail, the tail pointer to the successor node to the first node.

Fourth, linked lists and Sentinel

  • The use of sentinel simplify difficult to achieve

    First, let's look at the list of insert and delete operations. If we add a new node in the back node p, only the following two lines of code can get.

    new_node->next = p->next;
    p->next = new_node;

    However, when we want to insert an empty list a node can not write this. We need special treatment

    if(head==null){
        head = new_node;
    }

    This time, we introduce a blank node does not store data, whether the list is not empty, head point to it, we have put this list called sentinel nodes 带头链表. With sentinel node we can achieve unity insert and delete operations no special deal.

  • The combined use of sentinel orderly list

    // 有序链表的合并
    /**
    *Definition for singly-linked list
    *public class ListNode{
    *    int val;
    *    ListNode next;
    *    ListNode(int x){ val=x; }
    * }
    */
    public ListNode mergeTwoLists(ListNode l1, ListNode l2){
        ListNode soldier = new ListNode(0);
        ListNode p = soldier;
    
        while( l1 != null && l2 != null){
            if( l1.val < l2.val ){
                p.next = l1;
                l1 = l1.next;
            }
            else{
                p.next = l2;
                l2 = l2.next;
            }
            p = p.next;
        }
    
        if(l1!=null) { p.next = l1; }
        if(l2!=null) { p.next = l2; }
        return soldier.next;
    }

    There are many similar applications are not limited to the list, merge sort in other places also has applications waiting to be discovered!

Fifth, choose an array or a linked list?

  1. Insert, delete, and time complexity of random access
    arrays: insert, delete time complexity is O (n), a random access time complexity is O (1).
    List: insert, delete time complexity is O (1), a random access time complexity end is O (n).
  2. An array of shortcomings
    1) If the application for a lot of memory space, such as 100M, but if there is no continuous space 100M memory space, the application will fail, even though the available memory space over 100M.
    2) size is fixed, if storage space is insufficient, the need for expansion, expansion will be carried out once the data replication, but this time a very time-consuming.
  3. List the shortcomings
    1) consume more memory space, because of the extra space to store pointer information.
    2) the list of frequent insertions and deletions, cause frequent memory allocation and release, likely to cause memory fragmentation, if the Java language, may also cause frequent GC (automatic garbage collector) operation.
  4. how to choose?
    An array of easy to use, continuous use of memory space on implementation, can make use of data buffering CPU pre-reading group, so access to higher efficiency, but the list is not continuously stored in memory, so the CPU cache unfriendly, no way to read ahead.
    If the code memory usage is very demanding, it is more suitable for an array.

Guess you like

Origin www.cnblogs.com/jiaweixie/p/11307309.html