Implement a simple notebook list-the notebook list of Gao Qi JAVA300, and listen listjava300
In fact, the code of the old master is rough. For example, the header and end nodes are not fully considered. However, it is good to try again to understand the principle of the worker list. Implemented the add (), get (), size (), remove () and other methods.
The sorted list is also ordered and repeatable. The underlying implementation is the linked list, which is not safe and efficient. Slow query, fast modification, insertion, and deletion.
It is easier to draw several nodes on the paper during 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; // last Node 6 Object obj; // Object 7 Node next of this Node; // next Node 8 9 // 10 public Node () {11} 12 13 // 14 public Node with parameter Constructor (Node previous, Object obj, Node next) {15 super (); 16 this. previous = previous; 17 this. obj = obj; 18 this. next = next; 19} 20 21/** 22 * below are the getter and setter Methods 23 * @ return24 */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 write our own listing class:
1 package cn. bjsxt. collection; 2 3 public class sxt0000list {4 private Node first; // header Node 5 private Node last; // tail Node 6 7 private int size; // resize list size 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 both the first and last 21} else {22 // directly to las Add a new node 23 n after the t node. 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); // set the new node to the next node of the current last node 28 29 last = n; // The new node becomes the last node 30 31} 32 size ++; 33} 34 35 // Insert a new Object 36 public void add (int index, Object obj) {37 rangeCheck (index) at the specified position ); // check whether the array subscript exceeded 38 Node temp = node (index); // Node 39 40 Node newNode = new Node (); 41 newNode at the specified position. obj = obj; 42 43 if (temp! = Null) {44 Node up = temp. previous; // The Last node of the original node is 45 up. next = newNode; // The new node is set to the next node of the previous node 46 newNode. previous = up; // The Last node is set as the previous node 47 48 newNode of the new node. next = temp; // The original node is set as the next node of the new node 49 temp. previous = newNode; // set the new node to the previous node of the original node 50 51 size ++; 52} 53} 54 55 // return the linked list size 56 public int size () {57 return size; 58} 59 60 // array subscript 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 // return the element 72 public Object get (int index) {73 rangeCheck (index) at the specified position ); // check whether the array subscript exceeded 74 75 Node temp = node (index); 76 if (temp! = Null) {77 return temp. obj; 78} 79 return null; 80} 81 82 // return the 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 96 public void remove (int index) at the specified position) {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 sxt1_list list = new sxt1_list (); 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}