Java数据结构的实现
1.基于数组的链表
1 package array; 2 3 import java.util.Arrays; 4 5 /** 6 * 基于数组的链表 7 * 8 * @author 王彪 9 * 10 */ 11 public class MyArrayList { 12 private static Object[] elements = null; 13 private static int size = 0; 14 private static final int DEFUAT_INITIAL_CAPACITY = 10; 15 16 public MyArrayList() { 17 this(10); 18 } 19 20 public MyArrayList(int initialCapacity) { 21 if (initialCapacity < 0) { 22 throw new IllegalArgumentException("输入容量有误"); 23 } 24 elements = new Object[initialCapacity]; 25 } 26 27 //初始化数组 28 // 保存新元素 29 public void add(Object ele) { 30 // 数组的扩容问题 31 if (size == elements.length) { 32 Object[] temp = Arrays.copyOf(elements, elements.length * 2); 33 elements = temp; 34 } 35 elements[size] = ele; 36 size++; 37 } 38 39 public Object get(int index) { 40 if (index < 0 || index >= size) { 41 throw new IllegalArgumentException("输入索引有误"); 42 } 43 return elements[index]; 44 } 45 46 public void set(int index, Object newPlayerNum) { 47 if (index < 0 || index >= size) { 48 throw new IllegalArgumentException("输入索引有误"); 49 } 50 elements[index] = newPlayerNum; 51 } 52 53 public void remove(int index) { 54 if (index < 0 || index >= size) { 55 throw new IllegalArgumentException("输入索引有误"); 56 } 57 for (int i = index; i < size - 1; i++) { 58 elements[i] = elements[i + 1]; 59 } 60 elements[size - 1] = null; 61 size--; 62 } 63 64 public String toString() { 65 if (elements == null) { 66 return null; 67 } 68 if (size == 0) { 69 return "[]"; 70 } 71 StringBuilder sb = new StringBuilder(size * 3 + 1); 72 sb.append("["); 73 for (int i = 0; i < size; i++) { 74 sb.append(elements[i]); 75 if (i < size - 1) { 76 sb.append(","); 77 } 78 if (i == size - 1) { 79 sb.append("]"); 80 } 81 } 82 return sb.toString(); 83 } 84 85 public int size() { 86 return size; 87 } 88 89 public boolean isEmpty() { 90 return size == 0; 91 } 92 93 public void clear() { 94 this.elements = new Object[DEFUAT_INITIAL_CAPACITY]; 95 size = 0; 96 } 97 98 }
2.双向链表
1 package linked; 2 3 public class MyLinkedArrayList { 4 protected Node first; 5 protected Node last; 6 protected int size; 7 8 public void addFirst(Object ele) {// 在头部增加 9 Node node = new Node(ele); 10 if (size == 0) { 11 this.first = node; 12 this.last = node; 13 } else { 14 node.next = this.first; 15 this.first.prev = node; 16 this.first = node; 17 } 18 size++; 19 } 20 21 public void addLast(Object ele) {// 在尾部增加 22 Node node = new Node(ele); 23 if (size == 0) { 24 this.first = node; 25 this.last = node; 26 } else { 27 this.last.next = node; 28 node.prev = this.last; 29 this.last = node; 30 } 31 size++; 32 } 33 34 public void remove(Object ele) { 35 Node current = this.first; 36 for (int i = 0; i < size; i++) { 37 if (!current.ele.equals(ele)) { 38 if (current.next == null) { 39 return; 40 } 41 current = current.next; 42 } 43 } 44 if (current == this.first) { 45 this.first = current.next; 46 this.first.prev = null; 47 } else if (current == this.last) { 48 this.last = current.prev; 49 this.last.next = null; 50 } else { 51 current.prev.next = current.next; 52 current.next.prev = current.prev; 53 } 54 size--; 55 } 56 57 @Override 58 public String toString() { 59 if (size == 0) { 60 return "[]"; 61 } else { 62 StringBuilder sb = new StringBuilder(size * 3 + 1); 63 Node current = this.first;// 第一个节点 64 sb.append("["); 65 for (int i = 0; i < size; i++) { 66 sb.append(current.ele); 67 if (i != size - 1) { 68 sb.append(","); 69 } else { 70 sb.append("]"); 71 } 72 current = current.next; 73 } 74 return sb.toString(); 75 } 76 } 77 78 public class Node { 79 public Node(Object ele) { 80 this.ele = ele; 81 } 82 83 Node prev; 84 Node next; 85 public Object ele; 86 } 87 }
3.双向队列
1 package deque; 2 3 import linked.MyLinkedArrayList; 4 5 public class MyDeque extends MyLinkedArrayList { 6 public Object getFirst() { 7 return this.first.ele; 8 } 9 10 public Object getLast() { 11 return this.last.ele; 12 } 13 14 public void removeFirst(Object ele) { 15 super.remove(this.first); 16 } 17 18 public void removeLast(Object ele) { 19 super.remove(this.last); 20 } 21 22 public void addFirst(Object ele) { 23 super.addFirst(ele); 24 } 25 26 public void addLast(Object ele) { 27 super.addLast(ele); 28 } 29 30 }
4.堆
1 package stack; 2 3 import array.MyArrayList; 4 5 //栈 6 public class MyStack extends MyArrayList { 7 // 入栈 8 public void push(Object ele) { 9 super.add(ele); 10 } 11 12 // 删除栈顶元素 13 public void pop() { 14 int index = super.size() - 1; 15 super.remove(index); 16 } 17 18 // 查询栈顶元素 19 public Object peek() { 20 int index = super.size() - 1; 21 return super.get(index); 22 } 23 24 }