Some functions of LinkedList handwritten source code

Today, the epidemic is getting better. The moon is around 15 years old. Remember to watch the moon at night

If you have problems with your rough opinions, you can point out how much progress you can make at any time

The bottom layer of LinkedList is linked list structure
First of all, we need to understand what is the chain list structure, as shown in the figure below

Basically, the above figure means that each node code can:

public class Node { //

     private Node previous; //Previous node
     private  Node next;  //Next node
     private Object element; //Element data

    public Node(Object element) { //Parametric structure
        this.element = element;
    }

    public Node getPrevious() {
        return previous;
    }

    public void setPrevious(Node previous) {
        this.previous = previous;
    }

    public Node getNext() {
        return next;
    }

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

    public Object getElement() {
        return element;
    }

    public void setElement(Object element) {
        this.element = element;
    }
}

This is a handwritten part of the source code, which can be written by yourself

Relatively simple function

public class MyLinkedList {
     private Node first; //First element
     private Node end; //Last element
     private int  size;  //length

     //New method
     public void add(Object obj){
         Node node = new Node(obj);
         if(first == null){
             //node.setPrevious(null); / / previous element is null
              //node.setNext(null); / / the next element is also null

             first =node;
             end =node;
         }else{
             node.setPrevious(end); //Previous element assignment of the store
             node.setNext(null);  //Next element assignment of the store
             end.setNext(node); //In this case, end is the next to last value
             end = node; //Assign the last node to end

         }
         size++;
     }

     //Add element at specified location
     public void add(int index,Object obj){
         Node node = getNode(index);
         Node newNode =  new Node(obj);
         if(node != null){
             Node up = node.getPrevious();
             if(up !=null){
                 up.setNext(newNode);
             }
             node.setPrevious(newNode);
             newNode.setNext(node);
             if(index ==0){ //If you change the first value
                 first.setPrevious(newNode);
                 newNode.setNext(first);
                 first =newNode;
             }

             if(index == size-1){//If you change the last value
                 end = newNode;
             }
             size++;
         }
     }
     //get method
     public Object get(int index){
         Node temp = null;

         return  temp.getElement();
     }


     //Get node according to id
     public Node getNode(int index){
         Node temp = null;
         if(index<0 || index>size-1){
             throw new RuntimeException("Subscript boundary crossing"+index);
         }
         if(index<size>>1){//Speed up query efficiency
             temp =first;
             for (int i = 0; i <index; i++) {
                 temp = temp.getNext();
             }

         }else{
             temp =end;
             for (int i = size-1; i>index; i--) {
                 temp = temp.getPrevious();
             }

         }
         return  temp;
     }

     //Remove Method
     public void remove(int index){
         Node node = getNode(index);
         if(node != null) {

             Node up = node.getPrevious(); //Previous element
             Node down = node.getNext(); //Next element

             if (up != null) {
                 up.setNext(down);
             }
             if (down != null) {
                 down.setPrevious(up);
             }

             if (index == 0) { //If it's the first element
                 first = down;
             }
             if (index == size - 1) { //If it's the last element
                 end = up;
             }

         }
     }



     //Override toString method
    @Override
    public String toString() {
       Node temp = first;
       StringBuilder strb = new StringBuilder("[");
       while (temp!=null){
           strb.append(temp.getElement()+",");
     //      System.out.println(temp.getElement());
           temp =temp.getNext(); //Assign the next node to temp
       }
       strb.setCharAt(strb.length()-1,']');
       return strb.toString();
    }
}```


89 original articles published, 47 praised, 10000 visitors+
Private letter follow

Posted by EricS on Sun, 09 Feb 2020 06:13:15 -0800