Several common functions of handwritten two-way linked list

Posted by WormTongue on Tue, 03 Dec 2019 01:50:37 +0100

The functions are as follows:
1) create a linked list
2) add node (add by default and add by specified location)
3) access a node
4) delete node
5) get the length of the list
6) judge whether the list is empty
7) print format of custom linked list
8) clear the list

*Note: it is necessary to make clear the forward and subsequent nodes, and pay attention to the order of assignment when deleting!!!

Define Node class of Node in linked list


public class Node {
    
    /**
     * value
     * 
     */
    Object value;
    
    /**
     * precursor
     */
    Node pre;
    
    /**
     * Successor
     */
    Node next;
    
    public Node(){
        
    }
    
    public Node(Object value, Node pre, Node next) {
        this.value = value;
        this.pre = pre;
        this.next = next;
    }
}

Define a two-way linked list, with the following functions:

/**
 * Two way linked list
 * 
 * @author min
 *
 */
public class LinkList {

    /**
     * Head node
     */
    private Node head;
    
    /**
     * Tail node
     */
    private Node tail;
    
    private int size;
    
    public LinkList() {
        head = new Node();
        tail = new Node();
        
        head.next = tail;
        tail.pre = head;
        size = 0;
        
    }
    
    public int size() {
        return size;
    }
    
    public boolean isEmpty() {
        return size==0;
    }
    
    public void clear() {
        head.next = tail;
        tail.pre = head;
        size = 0;
    }
    
    /**
     * Add new data at the end
     * 
     * @param value data
     */
    public void add(Object value) {
        Node node = new Node(value,tail.pre,tail);
        tail.pre.next = node;
        tail.pre = node;
        size ++;
        }
    
    /**
     * Create a new node in a specific location
     * @param index
     * @param value
     */
    public void add(int index, Object value) {
        checkLinkList(index);
        
        if(index == size -1) {
            //Insert last
            add(value);
        }
        else{
            Node x = node(index-1);
            Node y = node(index);
            Node node = new Node(value, x, y);
            x.next = node;
            y.pre = node;
            size ++;
        }        
    }
    
    /**
     * Get nodes in a specific location
     * @param index
     * @return Node stored value
     */
    public Object get(int index) {
        //Check whether it is in the linked list
        checkLinkList(index);
        //Node value
        return node(index).value;
    }

    /**
     * Delete specific nodes
     * @param index
     * @return Deleted node
     */
    public Node remove(int index) {
        checkLinkList(index);
        Node x = node(index);
        x.pre.next = x.next;
        x.next.pre = x.pre;
        size --;
        return x;
    }
    

    /**
     * Check whether the index is in the linked list
     * 
     * @param index
     */
    private void checkLinkList(int index) {
        if(index > size() ||index < 0)
            throw new ArrayIndexOutOfBoundsException(index);
    }

    /**
     * Traverse linked list to query specific nodes
     * 
     * @param index Indexes
     * @return Specified node
     */
    private Node node(int index) {
        //1st node
        Node firstNode = head;
        //Last node
        Node lastNode = tail;
        
        //Traverse from the beginning
        if(index <=(size>>1) ) {
            Node indexNode = firstNode;
            for(int i = -1; i < index; i++)
                indexNode = indexNode.next;
            return indexNode;
        }
            
        //Traversal from tail
        else{            
            Node indexNode = lastNode;
            for(int i = size; i>index; i--) {
                indexNode = indexNode.pre;
            }
            return indexNode;
        }        
    }
    
    /**
     * Rewrite the output mode of linked list
     * 
     */
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        String str = "";
        for(int i = 0; i<size; i++) {
            str = String.valueOf(i + ":"+ node(i).value + "\n");
            builder.append(str);
        }
        return builder.toString();
    }    
}

test

Only some common functions are implemented. Compared with the classes in the toolkit, you can get a lot from them. Benefit a lot~

Topics: Java