publicclassArray<E>{private E[] data;privateint size;@SuppressWarnings("unchecked")publicArray(int capacity){
data =(E[])newObject[capacity];
size =0;}publicArray(){this(10);}publicintgetSize(){return size;}publicbooleanisEmpty(){return size ==0;}publicintgetCapacity(){return data.length;}publicvoidadd(int index, E e){if(index <0|| index > size){thrownewIllegalArgumentException("add failed.Index is illegal.");}if(size == data.length){resize(data.length *2);}if(index < size){for(int i = size -1; i >= index; i--){
data[i +1]= data[i];}}
data[index]= e;
size++;}publicvoidaddFirst(E e){add(0, e);}publicvoidaddLast(E e){add(size, e);}privatevoidresize(int newCapacity){@SuppressWarnings("unchecked")
E[] newData =(E[])newObject[newCapacity];for(int i =0; i < size; i++){
newData[i]= data[i];}
data = newData;}public E get(int index){if(index <0|| index >= size){thrownewIllegalArgumentException("get failed.Index is illegal.");}return data[index];}public E getFirst(){returnget(0);}public E getLast(){returnget(size -1);}publicvoidset(int index, E e){if(index <0|| index >= size){thrownewIllegalArgumentException("set failed.Index is illegal.");}
data[index]= e;}publicbooleancontains(E e){int ret =find(e);if(ret !=-1){returntrue;}returnfalse;}publicintfind(E e){for(int i =0; i < size; i++){if(e.equals(data[i])){return i;}}return-1;}public E remove(int index){if(index <0|| index >= size){thrownewIllegalArgumentException("remove failed.Index is illegal.");}
E ret = data[index];for(int i = index; i < size -1; i++){
data[i]= data[i +1];}
data[size -1]= null;
size--;if(size == data.length /4&& data.length /2!=0){resize(data.length /2);}return ret;}public E removeFirst(){returnremove(0);}public E removeLast(){returnremove(size -1);}public String toString(){
StringBuilder ret =newStringBuilder();
ret.append(String.format("Array: size = %d, capacity = %d\n", size, data.length));
ret.append("[");for(int i =0; i < size; i++){
ret.append(data[i]);if(i != size -1){
ret.append(", ");}}
ret.append("]");return ret.toString();}}
链表 LinkedList
底层实现也是动态数据结构
设置了虚拟头结点dummyHead
publicclassLinkedList<E>{classNode{public E e;public Node next;publicNode(E e, Node node){this.e = e;this.next = node;}publicNode(E e){this(e, null);}}private Node dummyHead;privateint size;publicLinkedList(){
dummyHead =newNode(null, null);
size =0;}publicintgetSize(){return size;}publicbooleanisEmpty(){return size ==0;}publicvoidadd(int index, E e){if(index <0|| index > size){thrownewIllegalArgumentException("Add failed. Index is illegal");}
Node prev = dummyHead;for(int i =0; i < index; i++){
prev = prev.next;}
prev.next =newNode(e, prev.next);
size++;}publicvoidaddFirst(E e){add(0, e);}publicvoidaddLast(E e){add(size, e);}public E get(int index){if(index <0|| index >= size){thrownewIllegalArgumentException("Get failed. Index is illegal");}
Node cur = dummyHead.next;for(int i =0; i < index; i++){
cur = cur.next;}return cur.e;}public E getFirst(){returnget(0);}public E getLast(){returnget(size -1);}publicvoidset(int index, E e){if(index <0|| index >= size){thrownewIllegalArgumentException("Set failed. Index is illegal");}
Node cur = dummyHead.next;for(int i =0; i < index; i++){
cur = cur.next;}
cur.e = e;}publicbooleancontains(E e){
Node cur = dummyHead;for(int i =0; i < size; i++){
cur = cur.next;if(cur.e.equals(e)){returntrue;}}returnfalse;}public E remove(int index){if(isEmpty()){thrownewIllegalArgumentException("Cannot remove from an empty list");}if(index <0|| index >= size){thrownewIllegalArgumentException("Remove failed. Index is illegal");}
Node prev = dummyHead;for(int i =0; i < index; i++){
prev = prev.next;}
Node retNode = prev.next;
prev.next = retNode.next;
retNode.next = null;
size--;return retNode.e;}public E removeFirst(){returnremove(0);}public E removeLast(){returnremove(size -1);}public String toString(){
StringBuilder ret =newStringBuilder();
Node cur = dummyHead;for(int i =0; i < size; i++){
cur = cur.next;
ret.append(cur.e +"-->");}
ret.append("NULL");return ret.toString();}}
队列 Queue FIFO
publicinterfaceQueue<E>{intgetSize();booleanisEmpty();voidenqueue(E e);
E dequeue();
E getFront();}
用数组实现Queue
publicclassArrayQueue<E>implementsQueue<E>{private Array<E> array;publicArrayQueue(int capacity){
array =newArray<>(capacity);}publicArrayQueue(){
array =newArray<>();}publicintgetSize(){return array.getSize();}publicbooleanisEmpty(){return array.isEmpty();}publicintgetCapacity(){return array.getCapacity();}publicvoidenqueue(E e){
array.addLast(e);}public E dequeue(){return array.removeFirst();}public E getFront(){return array.getFirst();}public String toString(){
StringBuilder ret =newStringBuilder();
ret.append("Queue: front [");for(int i =0; i < array.getSize(); i++){
ret.append(array.get(i));if(i != array.getSize()-1){
ret.append(", ");}}
ret.append("] tail");return ret.toString();}}
publicclassLoopQueue<E>implementsQueue<E>{private E[] data;privateint front, tail;privateint size;@SuppressWarnings("unchecked")publicLoopQueue(int capacity){
data =(E[])newObject[capacity +1];
front = tail =0;
size =0;}publicLoopQueue(){this(10);}publicintgetSize(){return size;}publicbooleanisEmpty(){return front == tail;}publicintgetCapacity(){return data.length -1;}publicvoidenqueue(E e){if((tail +1)% data.length == front){resize(getCapacity()*2);}
data[tail]= e;
tail =(tail +1)% data.length;
size++;}privatevoidresize(int newCapacity){@SuppressWarnings("unchecked")
E[] newData =(E[])newObject[newCapacity +1];for(int i =0; i < size; i++){
newData[i]= data[(i + front)% data.length];}
data = newData;
front =0;
tail = size;}public E dequeue(){if(isEmpty()){thrownewIllegalArgumentException("Queue is empty.");}
E ret = data[front];
data[front]= null;
front =(front +1)% data.length;
size --;if(size ==getCapacity()/4&&getCapacity()/2!=0){resize(getCapacity()/2);}return ret;}public E getFront(){if(isEmpty()){thrownewIllegalArgumentException("Queue is empty.");}return data[front];}public String toString(){
StringBuilder ret =newStringBuilder();
ret.append(String.format("LoopQueue: size = %d, capacity = %d\n", size,getCapacity()));
ret.append("front [");for(int i = front; i != tail; i =(i +1)% data.length){
ret.append(data[i]);if(i !=(front + size -1)% data.length){
ret.append(", ");}}
ret.append("] tail");return ret.toString();}}
用链表实现Queue
publicclassLinkedListQueue<E>implementsQueue<E>{privateclassNode{public E e;public Node next;publicNode(E e, Node next){this.e = e;this.next = next;}publicNode(E e){this(e, null);}public String toString(){return e.toString();}}private Node head, tail;privateint size;publicLinkedListQueue(){
head = null;
tail = null;
size =0;}publicintgetSize(){return size;}publicbooleanisEmpty(){return size ==0;}publicvoidenqueue(E e){
Node node =newNode(e);if(head == null){
head = tail = node;}else{
tail.next = node;
tail = node;}
size++;}public E dequeue(){if(isEmpty()){thrownewIllegalArgumentException("Cannot dequeue from an empty queue.");}
Node retNode = head;
head = retNode.next;
retNode.next = null;if(head == null){
tail = null;}
size--;return retNode.e;}public E getFront(){if(isEmpty()){thrownewIllegalArgumentException("Queue is empty.");}return head.e;}public String toString(){
StringBuilder sb =newStringBuilder();
sb.append("Queue: front ");
Node cur = head;while(cur != null){
sb.append(cur +"-->");
cur = cur.next;}
sb.append("NULL tail");return sb.toString();}}
栈 Stack LIFO
publicinterfaceStack<E>{intgetSize();booleanisEmpty();voidpush(E e);
E pop();
E peek();}
用数组实现Stack
publicclassArrayStack<E>implementsStack<E>{private Array<E> array;publicArrayStack(){
array =newArray<>();}publicArrayStack(int capacity){
array =newArray<>(capacity);}publicintgetSize(){return array.getSize();}publicbooleanisEmpty(){return array.isEmpty();}publicvoidpush(E e){
array.addLast(e);}public E pop(){return array.removeLast();}public E peek(){return array.getLast();}public String toString(){
StringBuilder ret =newStringBuilder();
ret.append("Stack: [");for(int i =0; i < array.getSize(); i++){
ret.append(array.get(i));if(i != array.getSize()-1){
ret.append(", ");}}
ret.append("] top");return ret.toString();}}
用链表实现Stack
publicclassLinkedListStack<E>implementsStack<E>{private LinkedList<E> list;publicLinkedListStack(){
list =newLinkedList<>();}publicintgetSize(){return list.getSize();}publicbooleanisEmpty(){return list.isEmpty();}publicvoidpush(E e){
list.addFrist(e);}public E pop(){return list.removeFirst();}public E peek(){return list.getFirst();}public String toString(){
StringBuilder res =newStringBuilder();
res.append("Stack: top ");
res.append(list);return res.toString();}}