Data Structure of 1 Java Learning Notes-Single Linked List

Posted by janey on Tue, 16 Jul 2019 00:41:36 +0200

Two days ago, in the process of job interview, I was asked about binary tree and algorithm related by a big data company. I didn't learn it well in college. After several years of work, I usually use less, but I can't write it down.

Because work always starts with Array, ArrayList, HashMap and HashSet, no matter what type, as if there were no other structures used. Although I know it's wrong, I don't care about the laziness. Recently, the sense of crisis is very serious, because slowly found that although there will be more and more frameworks, matching more and more slippery, database table design is becoming more and more standardized, but always feel unstable. This sense of instability comes from the ignorance of the underlying implementation of the framework and the lack of data structure.

If the data structure is not well studied, it will lead to errors in the selection of coding, low performance and large memory. If the framework is not thorough and the design pattern is not perfect, it will lead to high coupling between system modules, difficult to modify, and unable to do large projects.

So I decided to pick up the knowledge I left behind and learn it again from 0. Find a book, according to the book catalogue to learn one by one, record. For future self-check at any time.

The first one is a single linked list.

A single linked list is a simple, easy-to-understand data structure. That is, each node only needs to record its own value and the address of the next node. Like queuing in kindergartens, the teacher said that every student just needs to remember who is behind you. So the line can be lined up. Every time the children just need to find the person behind them and let him stand behind them.

The feature is that every time you want to find a node, you need to traverse from scratch, and the query efficiency is low. It is better to record the subscript position of the node by arrays and ArrayList, which can be found directly according to the subscript position. But insertion and deletion are efficient because only one node needs to be moved at the insertion location, while ArrayList needs to move all the elements behind the insertion location, pushing back a position as a whole.

Here we implement a simple single linked list model.

Define a node class Node, an attribute String to store values, and a Node object to store the next node.

package singlelink;

/**
 * Created by wuwf on 2017/3/25.
 */
public class Node {
    private String data;
    private Node next;

    public Node(String data) {
        this.data = data;
        next = null;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}

Define a LinkedList to represent the single linked list structure, which includes adding to the end, adding to a location, and so on.

package singlelink;

/**
 * Created by wuwf on 2017/3/25.
 * Singly Linked List
 */
public class LinkedList {
    private int size;
    /*
     * Header node
     */
    private Node head;

    public LinkedList() {
        size = 0;
    }

    public int size() {
        return size;
    }

    public void print() {
        if (size == 0) {
            System.out.println("Empty list");
            return;
        }
        while (head != null) {
            System.out.println(head.getData());
            head = head.getNext();
        }
    }

    /**
     * Append a node at the end
     *
     * @param node
     */
    public void add(Node node) {
        if (node == null) {
            return;
        }
        //If the list is empty, the new node is the head.
        if (size == 0) {
            head = node;
        } else { //Add to the end
            Node last = head;
            while (last.getNext() != null) {
                last = last.getNext();
            }
            last.setNext(node);
        }
        size++;
    }

    /**
     * Insert a node at a location
     *
     * @param index
     * @param node
     */
    public void insert(int index, Node node) {
        if (index < 0 || index > size) {
            throw new RuntimeException("Transboundary");
        }
        if (node == null) {
            return;
        }
        //plug part
        if (index == 0) {
            node.setNext(head);
            head = node;
        } else {
            Node tempNode = head;
            for (int i = 0; i < index - 1; i++) {
                //Get the parent node of the node to be inserted
                tempNode = tempNode.getNext();
            }
            node.setNext(tempNode.getNext());
            tempNode.setNext(node);
        }
        size++;
    }

}

In LinkedList, an attribute size is defined to record the total length of the list. There can be no null value in it.

The head represents the first node, and if the head is null, the list is empty.

The add method is to add a node at the end, which is better understood. This is done by traversing from head to tail and then setNext (the new node) at the tail node.

The insert method explains that a node is inserted somewhere. The method is to find the last node (parent node A) to be inserted, then take the new node B as the next node of A, and then take the next of the original A as the next of the new node B. It's easy to understand, and you need to make some overstepping and empty judgments when you write code.

I just wrote add and insert, so long as they understand, delete is actually the same step as add.

Topics: Attribute Big Data less Database