The underlying layer of LinkeList is implemented by linked list, which is slow to query, but convenient to add, delete and change
Here is my sketch of the chain list (funny face):
1. Add elements
/* * Test plus key elements!!! */ public void add(Object obj) { if(first == null) { //The first one is empty Node n = new Node(); n.previous = null; n.next = null; first = n; last = n; }else { //Add new nodes directly after last Node n = new Node(); //New a new node n.setPrevious(last); //The head of the new node is the last n.setObj(obj); //n is the value. n.setNext(null); //The tail of the new node is empty last.setNext(n); //Next to last is n last = n; //The last one is n } size++; //Every time you add one, size will add another } public int size() { return size; }
Analysis: ① if the link list is empty, that is, the content of the first link list is empty, then the content of n.next and n.previous are empty, so the first and last are both N;
(2) if there is content in the list, the content of the first list is not empty. You need to add a new node after the last one: the head of the new node is connected to the last one, the content is the element of the new home, the tail of the new node is empty, and the next one of the last is n.
2. Add element at specified location
/* * Add element emphasis at the specified location!! */ public void add(int index,Object obj) { Node temp = null; //Pointer, node if(first != null) { temp = first; //Object traversed by temp, starting from the first. for(int i = 0;i<index;i++) { temp = temp.next; //Give yourself the next one } } Node newNode = new Node(); newNode.obj = obj; if(temp !=null ) { Node up = temp.previous; //temp is the original node up.next = newNode; newNode.previous = up; newNode.next = temp; temp.previous = newNode; size++; } }
Analysis: traverse to find index;
The previous node (up) of the original node is next to the new node (up), and the previous node of the new node is up;
The next node of the new node is the original node, and the previous node of the original node is the new node.
3. get method
/* * Test get */ public Object get(int index) { rangeCheck1(index); Node temp = null; //Pointer, node if(first != null) { temp = first; //Object traversed by temp, starting from the first. for(int i = 0;i<index;i++) { temp = temp.next; //Give yourself the next one } } return temp.obj; }
It's relatively simple. I won't analyze it here
4. Delete element at specified location
/* * Delete object at specified location */ public void remove(int index) { Node temp = null; //Pointer, node if(first != null) { temp = first; //Object traversed by temp, starting from the first. for(int i = 0;i<index;i++) { temp = temp.next; //Give yourself the next one } } if(temp != null) { Node up = temp.previous; Node down = temp.next; up.next = down; down.previous = up; } size--; }
Analysis: traverse to find index
The previous (up) and next of the original node are connected to the next (down);
The next node of the original node is connected to the previous node