Advanced programmers lessons - Architect of the road (6) - list

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/m0_37609579/article/details/99618059

First, the list of definitions

[List] Baidu Encyclopedia (LinikedList) is a physical storage unit on a non-continuous, non-sequential storage structure , the data elements in a logical sequence by a linked list of pointers to achieve the link order. Chain by a series of nodes (each node element is called a linked list), with the node can be dynamically generated at runtime. Each node consists of two parts: a storage data element of the data field, the other is to store a next node address pointer field.

Compared to the linear form of the sequential structure , a complicated operation. Since the order is not necessarily stored in a linked list insertion can be achieved when O (1) complexity, than another linear sequence table faster table, but to find a node or access node identification number is required O (n) time, while the corresponding linear time complexity sequence table and the table are O (logn) and O (1).

The most obvious benefit is that the list, the conventional array arranged in the associated item may be different from the data items in the memory or disk sequential data access is often converted to a different arrangement in the order. List table allows the insertion and removal of an arbitrary position on the node , but does not allow random access . There are many lists of different types: one-way linked list , doubly linked list and circular list .

Second, to achieve a schematic view of a variety of list

Third, the list of operations

1. singly linked list

2. doubly linked list

3. circular list

Fourth, to achieve Josephus problem

Personal formed a circle n, the k first individual from a beginning one one clockwise packet number, the m individual report, make the column. And then from the next person clockwise from number 1 newspaper, report the m individual, then make it out of the line, ..., and so on, find the column order.

This problem can be converted into a circular list data structure problem. Specific abstract circular list is created, the output of the list, according to find the meaning of the questions that node to meet the requirements and delete, delete circulation process until only one element of a circular list, is the last element of the team.

 
  1.  
    /**
  2.  
    * 总人数n:7 从第几个开始k:3 报数淘汰m:3
  3.  
    * java 环形链表实现约瑟夫(Joseph)问题
  4.  
    */
  5.  
    public class JosephCycle {
  6.  
    public static void main(String []args){
  7.  
    CycleLink cl = new CycleLink();
  8.  
    cl.setLen(7);
  9.  
    cl.setK(3);
  10.  
    cl.setM(3);
  11.  
    cl.creatLink();
  12.  
    cl.play();
  13.  
    cl.show();
  14.  
    }
  15.  
     
  16.  
    }
  17.  
     
  18.  
    class Node{
  19.  
    int num;
  20.  
    Node nextNode = null;
  21.  
    public Node(int num){
  22.  
    this.num = num;
  23.  
    }
  24.  
    }
  25.  
     
  26.  
    class CycleLink{
  27.  
    int len;
  28.  
    int k ;
  29.  
    int m ;
  30.  
    Node firstNode = null;
  31.  
    Node temp = null;
  32.  
    public void setLen(int len){
  33.  
    this.len = len;
  34.  
    }
  35.  
     
  36.  
    public void setK(int k){
  37.  
    this.k = k;
  38.  
    }
  39.  
    public void setM(int m){
  40.  
    this.m =m;
  41.  
    }
  42.  
     
  43.  
    //创建链表
  44.  
    public void creatLink(){
  45.  
    for(int i = 1; i <= len ; i++){
  46.  
    // 处理首节点
  47.  
    if(i==1){
  48.  
    Node nd= new Node(i);
  49.  
    firstNode = nd;
  50.  
    temp = nd;
  51.  
    }else if(i == len){ //处理末节点
  52.  
    Node nd = new Node(i);
  53.  
    temp.nextNode = nd;
  54.  
    temp = nd;
  55.  
    temp.nextNode = firstNode;
  56.  
    }else{
  57.  
    Node nd = new Node(i);
  58.  
    temp.nextNode = nd;
  59.  
    temp = nd;
  60.  
    }
  61.  
    }
  62.  
    }
  63.  
     
  64.  
    public void play(){
  65.  
    temp = firstNode;
  66.  
    // 先找到编号为k的节点
  67.  
    for(int i = 1 ; i < k; i++){
  68.  
    temp = temp.nextNode;
  69.  
    }
  70.  
    while(this.len !=1){
  71.  
    //报数,找到m的上一个节点
  72.  
    for(int j = 1 ;j < m-1; j++){
  73.  
    temp = temp.nextNode;
  74.  
    }
  75.  
     
  76.  
    //因为少报了 1 ,所以将下一个节点删除,并从下下一个节点重新开始报数
  77.  
    System.out.println("要删除的节点: "+temp.nextNode.num);
  78.  
    /**
  79.  
    * 如果删除节点是firstNode,则将firstNode更新为下一个节点
  80.  
    * 注意不能用编号判断,因为新的编号对应的节点有可能又被删除
  81.  
    */
  82.  
    if(temp.nextNode==firstNode){
  83.  
    firstNode = temp.nextNode.nextNode;
  84.  
    }
  85.  
    temp.nextNode = temp.nextNode.nextNode;
  86.  
    temp = temp.nextNode;
  87.  
    //this.show();
  88.  
    this.len--;
  89.  
    }
  90.  
     
  91.  
    }
  92.  
     
  93.  
     
  94.  
    /**
  95.  
    * 遍历链表打印整个链表
  96.  
    */
  97.  
    public void show(){
  98.  
    temp = firstNode;
  99.  
    do{
  100.  
    System.out.println(temp.num);
  101.  
    temp = temp.nextNode;
  102.  
    }while(temp != firstNode);
  103.  
    }
  104.  
    }
 

operation result:

 
  1.  
    要删除的节点: 5
  2.  
    要删除的节点: 1
  3.  
    要删除的节点: 4
  4.  
    要删除的节点: 2
  5.  
    要删除的节点: 7
  6.  
    要删除的节点: 3
  7.  
    6
 

Five, Java class in the list

LinkedList nature class is a doubly linked list, the stack can be used as often, or queue it deque, the random insertion, be more efficient than random ArrayList class deleted. Especially for the header and trailer can be elements of the set for direct insertion and deletion, LinkedList provides a special method for the beginning and end of elements,

Six, abstract data type (ADT)

It refers to a mathematical model and a set of operations defined in the model. It depends only on its logical characteristics, and regardless of how to represent and implement the internal computer.

栈和队列这两种数据结构,我们可以分别使用数组和链表来实现,比如栈,对于使用者只需要知道pop()和push()方法或其它方法的存在以及如何使用即可,使用者不需要知道我们是使用的数组或是链表来实现的。

这在我们Java语言中的接口设计理念是相通的。

七、链表的应用场景

1、单向链接

单向链表适用于只从一端单向访问的场合,这种场合一般来说:

  1. 删除时,只适合删除第一个元素;
  2. 添加时,只直接添加到最后一个元素的后面或者添加到第一个元素的前面;
  3. 属于单向迭代器,只能从一个方向走到头(只支持前进或后退,取决于实现),查找效率极差。不适合大量查询的场合。

这种典型的应用场合是各类缓冲池和栈的实现。

2、双向链表

双向链表相比单向链表,拥有前向和后向两个指针地址,所以适合以下场合:

  1. 删除时,可以删除任意元素,而只需要极小的开销;
  2. 添加时,当知道它的前一个或后一个位置的元素时,只需要极小的开销。
  3. 属于双向迭代器,可以从头走到尾或从尾走到头,但同样查找时需要遍历,效率与单向链表无改善,不适合大量查询的场合。

这种典型的应用场景是各种不需要排序的数据列表管理。

八、总结

上面我们讲了各种链表,每个链表都包括一个LinikedList对象和许多Node对象,LinkedList对象通常包含头和尾节点的引用,分别指向链表的第一个节点和最后一个节点。而每个节点对象通常包含数据部分data,以及对上一个节点的引用prev和下一个节点的引用next,只有下一个节点的引用称为单向链表,两个都有的称为双向链表。next值为null则说明是链表的结尾,如果想找到某个节点,我们必须从第一个节点开始遍历,不断通过next找到下一个节点,直到找到所需要的。栈和队列都是ADT,可以用数组来实现,也可以用链表实现。


我的微信公众号:架构真经(id:gentoo666),分享Java干货,高并发编程,热门技术教程,微服务及分布式技术,架构设计,区块链技术,人工智能,大数据,Java面试题,以及前沿热门资讯等。每日更新哦!

参考文章:

  1. https://www.cnblogs.com/ysocean/p/7928988.html
  2. https://blog.csdn.net/baidu_37181928/article/details/80731350
  3. https://blog.csdn.net/qq_34478594/article/details/80351017
  4. https://www.cnblogs.com/lightandtruth/p/9468278.html

 

Guess you like

Origin www.cnblogs.com/anymk/p/11470499.html