import java.util.Arrays; public class Array<E> { private E[] data; private int size; //构造函数,传入数组的容量capacity构造Array public Array(int capacity) { this.data = (E[]) new Object[capacity]; size = 0; } //无参数构造函数 public Array() { this(10); } //获取数组的个数 public int getSize() { return size; } //获取数组的容量 public int getCapacity() { return data.length; } //数组是否为空 public boolean isEmpty() { return size == 0; } //添加最后一个元素 public void addLast(E e) { add(size,e); } //添加第一个元素 public void addFirst(E e){ add(0,e); } //获取inde索引位置的元素 public E get(int index){ if (index < 0 || index >= size){ throw new IllegalArgumentException("Get failed,index is illegal"); } return data[index]; } public void set(int index,E e){ if (index < 0 || index >= size){ throw new IllegalArgumentException("Get failed,index is illegal"); } data[index] = e; } //添加元素 public void add(int index,E e){ if (index > size || index < 0){ throw new IllegalArgumentException("add failed beceause index > size or index < 0,Array is full."); } if (size == data.length){ resize(data.length * 2); } for (int i = size - 1; i >= index; i--) { data[i+1] = data[i]; } data[index] = e; size ++; } //扩容数组 private void resize(int newCapacity) { E[] newData = (E[]) new Object[newCapacity]; for (int i = 0; i < size; i++) { newData[i] = data[i]; } data = newData; } public E[] getData() { return data; } //查找数组中是否有元素e public boolean contains(E e){ for (int i = 0; i < size; i++) { if (data[i].equals(e)){ return true; } } return false; } //根据元素查看索引 public int find(E e){ for (int i = 0; i < size; i++) { if (data[i].equals(e)){ return i; } } return -1; } //删除某个索引元素 public E remove(int index){ if(index < 0 || index >= size){ throw new IllegalArgumentException("detele is fail,index < 0 or index >= size"); } E ret = data[index]; for (int i = index + 1; i < size; i++) { data[i - 1] = data[i]; } size --; data[size] = null; if (size < data.length / 2){ resize(data.length / 2); } return ret; } //删除首个元素 public E removeFirst(){ return this.remove(0); } //删除最后一个元素 public E removeLast(){ return this.remove(size - 1); } //从数组删除元素e public void removeElemen(E e){ int index = find(e); if (index != -1){ remove(index); } } @Override public String toString() { StringBuilder sb = new StringBuilder(""); sb.append(String.format("Array:size = %d,capacity = %d ",size,data.length)); sb.append("["); for (int i = 0; i < size; i++) { sb.append(data[i]); if (i != size - 1){ sb.append(","); } } sb.append("]"); return sb.toString(); } }
队列
public class ArrayStack<E> implements Stack<E> { Array<E> array; public ArrayStack(int capacity) { array = new Array<>(capacity); } public ArrayStack() { this.array = new Array<>(); } @Override public int getSize() { return array.getSize(); } @Override public boolean isEmpty() { return array.isEmpty(); } @Override public void push(E e) { array.addLast(e); } @Override public E pop() { return array.removeLast(); } @Override public E peek() { return array.getLast(); } public int getCapacity(){ return array.getCapacity(); } @Override public String toString() { StringBuilder res = new StringBuilder(""); res.append("Stack:"); res.append("["); for (int i = 0; i < array.getSize(); i++) { res.append(array.get(i)); if ( i != array.getSize() - 1){ res.append(","); } } res.append("] top"); return res.toString(); } }
队列
public class ArrayQueue<E> implements Queue<E> { Array<E> array; public ArrayQueue(int capacity) { this.array = new Array<>(capacity); } public ArrayQueue() { this.array = new Array<>(); } @Override public int getSize() { return array.getSize(); } @Override public boolean isEmpty() { return array.isEmpty(); } @Override public void enqueue(E e) { array.addLast(e); } @Override public E dequeue() { return array.removeFirst(); } @Override public E getFront() { return array.getFirst(); } public int getCapacity(){ return array.getCapacity(); } @Override public String toString() { StringBuilder res = new StringBuilder(""); res.append("Queue ["); for (int i = 0; i < array.getSize(); i++) { res.append(array.get(i)); if (i != array.getSize() - 1){ res.append(","); } } res.append("] tail"); return res.toString(); } public static void main(String[] args){ ArrayQueue<Integer> arrayQueue = new ArrayQueue<>(); for (int i = 0; i < 10; i++) { arrayQueue.enqueue(i); System.out.println(arrayQueue); if (i % 3 == 2){ arrayQueue.dequeue(); System.out.println(arrayQueue); } } } }
第二种队列算法
public class LoopQueue<E> implements Queue<E> { private E[] data; private int front, tail; private int size; public LoopQueue(int capacity) { data = (E[]) new Object[capacity + 1]; } public LoopQueue() { this(10); } @Override public int getSize() { return size; } @Override public boolean isEmpty() { return size == 0; } public int getCapacity() { return data.length - 1; } //入队 @Override public void enqueue(E e) { if (front == (tail + 1) % data.length) { resize(getCapacity() * 2); } data[tail] = e; tail = (tail + 1) % data.length; size++; } private void resize(int newCapacity) { E[] newData = (E[]) new Object[newCapacity + 1]; for (int i = 0; i < size; i++) { newData[i] = data[(i + 1) % data.length]; } data = newData; front = 0; tail = size; } //出队 @Override public E dequeue() throws Exception { if (isEmpty()) { throw new Exception("LoopQueue is isEmpty,dequeue is fail."); } E ret = data[front]; front = (front + 1) % data.length; size--; if (getCapacity() / 4 == size && getCapacity() / 2 != 0) { resize(getCapacity() / 2); } return ret; } @Override public E getFront() throws Exception { return data[front]; } @Override public String toString() { StringBuilder res = new StringBuilder(""); res.append(String.format("LoopQueue:size = %d,getCapacity = %d ",size,getCapacity())); res.append("LoopQueue frot["); for (int i = front;i != tail;i = (i + 1) % data.length) { res.append(data[i]); if (tail != (i + 1) % data.length ) { res.append(","); } } res.append("] tail"); return res.toString(); } public static void main(String[] args) throws Exception { LoopQueue<Integer> loopQueue = new LoopQueue<Integer>(); for (int i = 0; i < 10; i++) { loopQueue.enqueue(i); if (i % 3 == 2) { loopQueue.dequeue(); } System.out.println(loopQueue); } } }