LinkedList list of principles and structure of the Java data source analysis section

First, why should learning data structure?

  • As a programmer, no matter what programming language you use, the data structure is to take the bottom of things. Like the equivalent Gailou foundation, the foundation is not good to do, no matter how good upside to no avail.
  • In high-level languages, these will generally based data structure encapsulates we learn to learn these basic things right?
    Of course, the only people who know these basic things, we can better use the language encapsulated api. To give a simple example, in Java, List implementation class has ArrayList, LinkedList, Vector, you know what with the highest efficiency under what circumstances do? Only you know the underlying source in order to better use.
  • How to learn the data structure?
    Can read books, watch videos, see the blog ... but the most important point, we must own hand to knock, such as myself to write a list of their own to simulate a stack, a queue and so on. Maybe you did not write the package in the language used so good, but you will definitely rewarding.
  • Video books where to find?
    No micro-channel public concern "small fish and Java", backstage reply 数据结构, I have already tidied data.
    Fish and Java

    Second, what is data and data structures?

    Data is a part of a combination of some or content relationships.
    Data structure is a data storage. Discussed from different angles, are classified as follows:
    | logical structure is divided | divided by the physical structure |
    || - |
    | linear structure | sequential storage structure |
    | set structure | Storage Structure |
    | || tree
    | graph structure ||
  • Logical structure, that is, in accordance with the people's logical thinking to its classification.
  • Is the physical structure of data is stored on disk, the storage area may be a single piece, or may be different memory blocks (but before they are related, so we divided into a set of data).

Second, the physical structure of the classification sub-method

Sequential storage

Is stored contiguously on disk, in Java is an array, it starts from the first index, all data is closely followed.

Chain store

These data (called data elements, is no longer separated) on the disk is stored separately, just because there is some relationship between them, so we will contact them to together to form a data structure ---- list.

Code

For sequential storage, the array is in Java, so do not do code.

For the chain store that is, any element which, in addition to storing the data itself, but also other storage location or a plurality of data storage elements. The main categories are below:

Singly linked list:

It is in this element, in addition to its own data store also stores its next data
Singly linked list

 class LinkedNode<T> {
        private T data;
        private LinkedNode<T> next;

        public LinkedNode(T data) {
            this.data = data;
            //在构建这个时,我们就让它的下一个指针为null
            next = null;
        }
    }

Next, write a class of its management, we write an add (T t) method, is inserted into the final

public class MyLinked<T> {
    /**
     * 用一个头指针来表示这个链表的头
     */
    private LinkedNode<T> first;

    public MyLinked() {
    }

    /**
     * 提供一个有参构造方法
     *
     * @param t
     */
    public MyLinked(T t) {
        this.first = new LinkedNode<>(t);
        first.next=null;
    }

    /**
     * 往链表中添加元素,默认是添加到最后的
     *
     * @param t
     */
    public void add(T t) {
        if (first == null) {
            first = new LinkedNode<>(t);
        } else {
            LinkedNode herd = first;
            while (herd.next != null) {
                herd = herd.next;
            }
            //当从这个循环中出来的时候,这个herd.next就是null,也就是说这个herd就是这个链表的最后一个元素
            herd.next = new LinkedNode(t);
        }
    }
}

We write this add method is to traverse the entire list to do this.
These are the simplest kind of a list, we use generics to make it more versatile.

Doubly linked list

Doubly linked list
Doubly linked list is stored in its own data in the linked list, it also stores the address of the previous and next data.

class LinkedNode<T> {
        private T data;
        private LinkedNode<T> prev;
        private LinkedNode<T> next;

        public LinkedNode(T data) {
            this.data = data;
            this.prev = null;
            this.next = null;
        }
    }

Table circular list

  • It is also divided into one-way and two-way circular linked list circular list
  • In the one-way linked list, the last element of the next is not null, but a pointer to the first element
  • In the doubly linked list prev its first element is the last element, the last element of a next is the first element.
  • Because it is a two-way cycle, so in one way than in the efficiency faster.
    For example, the length of the list is 50, we are looking for the first 48 elements. If you are single, it can only be from 0-> 1-> 2 .....-> 48, so to traverse the first 48 elements; if it is two-way, we need only 50-> 49-> 48, three times enough.
  • The underlying Java LinkedList is that two-way circular list. Let's take a look at its source code:
private static class Node<E> {
       E item;
       Node<E> next;
       Node<E> prev;

       Node(Node<E> prev, E element, Node<E> next) {
           this.item = element;
           this.next = next;
           this.prev = prev;
       }
   }

We mainly look at its most basic add method, to experience one of its charm

  • It has two methods add (E e) and add (int index, E e)

add (E e) is added to the final list, the final call as follows:

void linkLast(E e) {
    //将当前的最后一个节点保存下来
        final Node<E> l = last;
    //构造一个新的节点对象
        final Node<E> newNode = new Node<>(l, e, null);
    //将这个链表的last指向这个新元素
        last = newNode;
        if (l == null){
    //这个条件就是说,此时链表为空。因为l是在添加之前的last,如果这个链表为空,last肯定是空的
            first = newNode;
        }else{
            l.next = newNode;
    }
        size++;//当前的链表大小++
        modCount++;//这个是用来记录这个链表的操作次数,对这个链表进行的任何操作,这个都会++
    }

Top of the constructors

    Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }

This last is inserted into the list and did not go to a traverse the entire list, but will last.next point to the new node

add (int index, E e) is inserted to the specified position
the most important is the use of the circular list to find the index in the former half or the rear half of the main search code is as follows:

Node<E> node(int index) {
        if (index < (size >> 1)) {
    //上边的>>就是取半,除以2
            Node<E> x = first;
            for (int i = 0; i < index; i++)
                x = x.next;
            return x;
        } else {
            Node<E> x = last;
            for (int i = size - 1; i > index; i--)
                x = x.prev;
            return x;
        }
    }

We can see, it is judged that the part where were traversing different way, is to use simple dichotomy of time

  • Of course, it also wrote other internal iterative methods, etc., interested can look at themselves.

Guess you like

Origin www.cnblogs.com/Lyn4ever/p/12105106.html