This part of Lao Gao's code is actually written in a rough way. For example, the head node and the tail node are not considered in a comprehensive way, but it's good to type it again to understand the principle of LinkedList. The methods of add(), get(), size(), remove() are implemented.
LinkedList is also orderly and repeatable. The underlying implementation is linked list, which is unsafe and efficient. Slow query, fast modification, insertion and deletion.
It's better to draw a few nodes on the paper when writing.
First, the Node class is used to represent a Node:
1 package cn.bjsxt.collection; 2 3 //Used to represent a node 4 public class Node { 5 Node previous; //Previous node 6 Object obj; //Objects of this node 7 Node next; //Next node 8 9 //Parameterless constructor 10 public Node() { 11 } 12 13 //Parametrical constructor 14 public Node(Node previous, Object obj, Node next) { 15 super(); 16 this.previous = previous; 17 this.obj = obj; 18 this.next = next; 19 } 20 21 /** 22 * Here are getter and setter methods 23 * @return 24 */ 25 public Node getPrevious() { 26 return previous; 27 } 28 29 public void setPrevious(Node previous) { 30 this.previous = previous; 31 } 32 33 public Object getObj() { 34 return obj; 35 } 36 37 public void setObj(Object obj) { 38 this.obj = obj; 39 } 40 41 public Node getNext() { 42 return next; 43 } 44 45 public void setNext(Node next) { 46 this.next = next; 47 } 48 49 }
Then we wrote our own LinkedList class:
1 package cn.bjsxt.collection; 2 3 public class SxtLinkedList { 4 private Node first; //Head node 5 private Node last; //Tail node 6 7 private int size; //LinkedList Size of 8 9 //Add a node 10 public void add(Object obj) { 11 Node n = new Node(); 12 13 //If there is no first node 14 if(first == null) { 15 n.setPrevious(null); 16 n.setObj(obj); 17 n.setNext(null); 18 19 first = n; 20 last = n; //This node is the first and last 21 } else { 22 //Direct to last Add new node after node 23 n.setPrevious(last); //Set the last node as the previous node of the new node 24 n.setObj(obj); 25 n.setNext(null); 26 27 last.setNext(n); //The new node is set as the next node of the current last node 28 29 last = n; //New node becomes last node 30 31 } 32 size++; 33 } 34 35 //Inserts a new object at the specified location 36 public void add(int index, Object obj) { 37 rangeCheck(index); //Check whether array subscript is out of bounds 38 Node temp = node(index); //Specify nodes on location 39 40 Node newNode = new Node(); 41 newNode.obj = obj; 42 43 if(temp != null) { 44 Node up = temp.previous; //Previous node of the original node 45 up.next = newNode; //The new node is set as the next node of the previous node 46 newNode.previous = up; //Previous node set as previous node of new node 47 48 newNode.next = temp; //The original node is set as the next node of the new node 49 temp.previous = newNode; //The new node is set as the previous node of the original node 50 51 size++; 52 } 53 } 54 55 //Returns the size of the linked list 56 public int size() { 57 return size; 58 } 59 60 //Array subscript out of bounds check 61 private void rangeCheck(int index) { 62 if(index<0 || index>=size) { 63 try { 64 throw new Exception(); 65 } catch (Exception e) { 66 e.printStackTrace(); 67 } 68 } 69 } 70 71 //Returns the element at the specified location 72 public Object get(int index) { 73 rangeCheck(index); //Check whether array subscript is out of bounds 74 75 Node temp = node(index); 76 if(temp != null) { 77 return temp.obj; 78 } 79 return null; 80 } 81 82 //Returns the node at the specified location Node object 83 public Node node(int index) { 84 Node temp = null; 85 if(first != null) { 86 temp = first; 87 for(int i=0;i<index;i++) { 88 temp = temp.next; 89 } 90 } 91 return temp; 92 } 93 94 95 //Delete node at specified location 96 public void remove(int index) { 97 Node temp = node(index); 98 99 if(temp != null) { 100 Node up = temp.previous; 101 Node down = temp.next; 102 up.next = down; 103 down.previous = up; 104 size--; 105 } 106 107 108 } 109 110 public static void main(String[] args) { 111 SxtLinkedList list = new SxtLinkedList(); 112 list.add("aaa"); 113 list.add("bbb"); 114 list.add(1,"BBBB"); 115 System.out.println(list.size()); 116 System.out.println(list.get(2)); 117 // list.remove(1); 118 System.out.println(list.get(1)); 119 } 120 121 122 }