LinkedList手写1
LinkedList的底层是基于链表实现的,所以查询慢,增删改快,但是线程不安全,效率较高。
LinkedList类
1 /** 2 * remove方法 3 * add方法 4 * get方法 5 * getNode方法 6 * @author HQ 7 * @e-mail ahmashq95@gmail.com 8 * @date 2018/11/2. 9 */ 10 public class LinkedList01 { 11 12 private Node first; 13 private Node last; 14 private int size; 15 16 public void remove(int index) { 17 Node temp = getNode(index); 18 if (temp != null) { 19 Node up = temp.previous; 20 Node down = temp.next; 21 if (up != null) { 22 up.next = down; 23 } 24 if (down != null) { 25 down.previous = up; 26 } 27 28 //被删除的元素是第一个元素时 29 if(index==0){ 30 first=down; 31 } 32 //被删除的元素是最后一个元素时 33 if(index==size-1){ 34 last=up; 35 } 36 size--; 37 } 38 } 39 40 // [ ] 41 //["a","b"] 42 public void add(Object object) { 43 Node node = new Node(object); 44 45 if (first == null) { 46 47 node.previous = null; 48 node.next = null; 49 50 first = node; 51 last = node; 52 } else { 53 node.previous = last; 54 node.next = null; 55 56 last.next = node; 57 last = node; 58 } 59 size++; 60 } 61 62 @Override 63 public String toString() { 64 StringBuilder stringBuilder = new StringBuilder("["); 65 66 67 Node temp = first; 68 while (temp != null) { 69 stringBuilder.append(temp.element + ","); 70 temp = temp.next; 71 } 72 stringBuilder.setCharAt(stringBuilder.length() - 1, ']'); 73 74 return stringBuilder.toString(); 75 } 76 77 public Object get(int index) { 78 if (index < 0 || index > size - 1) { 79 throw new RuntimeException("索引位置不合法" + index); 80 } 81 82 Node temp = getNode(index); 83 return temp != null ? temp.element : null; 84 85 } 86 87 public Node getNode(int index) { 88 Node temp = null; 89 //size>>1 相当于除以2 90 if (index < (size >> 1)) { 91 temp = first; 92 for (int i = 0; i < index; i++) { 93 temp = temp.next; 94 } 95 96 } else { 97 temp = last; 98 for (int i = size - 1; i > index; i--) { 99 temp = temp.previous; 100 101 } 102 } 103 return temp; 104 105 } 106 107 public static void main(String[] args) { 108 LinkedList01 list01 = new LinkedList01(); 109 110 list01.add("a"); 111 list01.add("b"); 112 list01.add("c"); 113 list01.add("d"); 114 list01.add("e"); 115 list01.add("f"); 116 117 System.out.println(list01); 118 System.out.println("------------"); 119 list01.remove(0); 120 System.out.println(list01); 121 System.out.println(list01.size); 122 System.out.println("------------"); 123 124 list01.remove(0); 125 System.out.println(list01); 126 System.out.println(list01.size); 127 System.out.println("------------"); 128 129 list01.remove(0); 130 System.out.println(list01); 131 System.out.println(list01.size); 132 } 133 }
Node类![点击并拖拽以移动]()
1 /** 2 * @author HQ 3 * @e-mail ahmashq95@gmail.com 4 * @date 2018/11/2. 5 */ 6 public class Node { 7 Node previous;//Last Node 8 Node next;//next Node 9 Object element; 10 11 public Node(Node previous, Node next, Object element) { 12 super(); 13 this.previous = previous; 14 this.next = next; 15 this.element = element; 16 } 17 18 public Node(Object element) { 19 this.element = element; 20 } 21 }

浙公网安备 33010602011771号