Realize the idea of singly linked list and simple code

package com.algorithm.tiger.linkedlist; 

/** 
 * Linked list (composed of many nodes connected) 
 * 
 * @description: singly linked list 
 * @author: tiger 
 * @create: 2020-07-11 11:05 
 */ 
public class MyLinkedList { 

    // 
    Private Node head at the head of the 

    linked list ; // 
    Private Node tail at the end of the linked list ; 

    //The actual size of the linked list 
    private int size; 

    public Node find(int index) { 
        //Predict abnormal conditions 
        if (index <0 | | index> size) { 
            throw new IndexOutOfBoundsException("Out of the range of the linked list node!"); 
        } 

        if (size == 0) { 
            //Empty linked list 
            return null; 
        } else if (index == 0) {
            //Head 
        //Predict abnormal conditions
            return head; 

        } else if (size == index) { 
            //Tail 
            return tail; 
        } else { 
            //Middle (traverse from the head until reaching the Node node at index) 
            Node targetNode = head; 
            for (int i = 0; i <index; i++) { 
                targetNode = targetNode.next; 
            } 
            return targetNode; 
        } 
    } 

    /** 
     * Insert elements (head insertion, middle insertion, tail insertion) 
     * 
     * @param data 
     * @param index 
     * @return 
     */ 
    public Node insert(int data, int index) { 

        if (index <0 || index> size) {
            throw new IndexOutOfBoundsException("Out of the range of the linked list node!"); 
        } 

        //The inserted node 
        Node insertNode = new Node(data); 

        if (size == 0) { 
            //Empty linked list 
            head = insertNode; 
            tail = insertNode; 
        } else if (index == 0) { 
            //Head insertion 
            //1. Point the next pointer of the new node to the original head node 
            insertNode.next = head; 
            //2. Turn the new node into the head node of the linked list 
            head = insertNode ; 

        } else if (size == index) { 
            //Insert the tail 
            //1. Point the next node of the original tail node to the new node 
            tail = insertNode; 
        } else { 
            tail.next = insertNode;
            //2. Point the new node to the tail node 
            //Insert in the middle (rely on the lookup method of the linked list) 

            Node prevNode = find(index-1); 
            Node currentNode = prevNode.next; 
            prevNode.next = insertNode; 
            insertNode.next = currentNode ; 

            //Refresh from back to front 
// Node prevNode = find(index-1); 
// //1. The next pointer of the new node, which points to the node at the insertion position (1 and 2 cannot be reversed) 
// insertNode.next = prevNode.next; 
// 2. The next pointer of the pre-node at the insertion position, pointing to the new node 
// prevNode.next = insertNode; 

        } 
        size++; 
        //Return the inserted new node data 
        return insertNode; 
    } 

    /** 
     * Delete element, return the deleted node 
     *
     * @param index  
     * @return
     */ 
    public Node remove(int index) { 
        //Predict the abnormal situation 
        if (index <0 || index> size) { 
            throw new IndexOutOfBoundsException("Out of the range of the linked list node!"); 
        } 

        Node removeNode = null ; 
        if (size == 0) { 
            //empty linked list 

        } else if (index == 0) { 
            //remove the head 
            removeNode = head; 
            head = head.next; 
        } else if (size == index) { 
            // Remove the tail 
            Node prevNode = find(index-1); 
            removeNode = prevNode.next; 
            prevNode.next = null; 
            tail = prevNode; 
        } else { 
            //Delete in the middle (rely on linked list search method)
            Node prevNode = find(index - 1);
            Node nextNode = prevNode.next.next;
            removeNode = prevNode.next;
            prevNode.next = nextNode;
        }
        size--;
        return removeNode;
    }

    public Node getHead() {
        return head;
    }

    public void setHead(Node head) {
        this.head = head;
    }

    public Node getTail() {
        return tail;
    }

    public void setTail(Node tail) {
        this.tail = tail;
    }

    public int getSize() {
        return size;
    }
        public int getData() {

    public void setSize(int size) {
        this.size = size; 
    } 

    @Override 
    public String toString() { 
        return "MyNode{" + 
                "head=" + head + 
                ", tail=" + tail + 
                ", size=" + size + 
                '}'; 
    } 

    / ** 
     * Node internal class 
     */ 
    private static class Node { 
        //Current node data 
        private int data; 
        //Next node 
        private Node next; 

        public Node() { 
        } 

        public Node(int data) { 
            this.data = data; 
        } 

            return data;
        }

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

        public Node getNext() {
            return next;
        }

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

        @Override
        public String toString() {
            return "Node{" +
                    "data=" + data +
                    ", next=" + next +
                    '}';
        }
    }

    /**
     * 测试
     *
     * @param args
     */
    public static void main(String[] args) {
        Node node = new Node(12);

        MyLinkedList linkedList = new MyLinkedList();
        linkedList.insert(12, 0);
        linkedList.insert(122, 0);
        linkedList.insert(343, 0);
        linkedList.insert(1243, 1);
        linkedList.insert(444444, 1);
        linkedList.insert(45678, 1);
        linkedList.insert(333333, 1);
        System.out.println(linkedList);
        System.out.println("===================");
//        System.out.println(linkedList.find(2));
//        linkedList.remove(0);
//        linkedList.remove(0);
        linkedList.remove(3);
        System.out.println(linkedList);
        System.out.println("===================");
//        LinkedList linkedList1 = new LinkedList();
    }
}

Guess you like

Origin blog.csdn.net/qq_36336332/article/details/107292032