队列

基于数组的实现的队列

// 动态数组的封装
public class Array<E> {

private E[] data;
private int size;

/**
* 数组扩容
* @param newCapacity
*/
private void resize(int newCapacity) {
E[] newData = (E[])new Object[newCapacity];
for(int i = 0; i < size; i++) {
newData[i] = data[i];
}
data = newData;
}

/**
* 构造函数
* @param capacity
*/
public Array(int capacity) {
// 泛型数组无法直接new出
data = (E[])new Object[capacity];
size = 0;
}

/**
* 无参构造函数,初始化数组容量为10
*/
public Array() {
this(10);
}

/**
* 获取数组大小
* @return
*/
public int getSize() {
return size;
}

/**
* 获取数组容量
* @return
*/
public int getCapacity() {
return data.length;
}

/**
* 判空
* @return
*/
public boolean isEmpty() {
return size == 0;
}

/**
* 末尾添加元素
* @param e
*/
public void addLast(E e) {
add(size, e);
}

/**
* 在头插入元素
* @param e
*/
public void addFirst(E e) {
add(0, e);
}

/**
* 在某一位置添加元素
* @param index
* @param e
*/
public void add(int index, E e) {
if(size == data.length) {
resize(2 * data.length);
}else if(index < 0 || index > size){
throw new IllegalArgumentException("容量错误!");
}else{
for(int i = size - 1; i >= index; i-- ){
data[i + 1] = data[i];
}
data[index] = e;
size++;
}
}

/**
* 获取index索引的元素
* @param index
* @return
*/
public E get(int index) {
if(index < 0 || index >= size) {
throw new IllegalArgumentException("容量错误!");
}
return data[index];
}

public E getLast() {
return get(size);
}

public E getFirst() {
return get(0);
}

/**
* 设置某一位置的元素
* @param index
* @param e
*/
public void set(int index, E e) {
if(index < 0 || index >= size){
throw new IllegalArgumentException("容量错误!");
}
data[index] = e;
}

/**
* 某一个元素是否包含
* @param e
* @return
*/
public boolean contains(E e) {
for(int i = 0; i < size; i++) {
if(data[i].equals(e)){
return true;
}
}
return false;
}

/**
* 返回某一元素的索引下标
* @param e
* @return
*/
public int find(E e) {
for(int i = 0; i < size; i++) {
if(data[i].equals(e)) {
return i;
}
}
return -1;
}

/**
* 删除某一元素
* @param index
* @return
*/
public E remove(int index) {
if(index < 0 || index >= size){
throw new IllegalArgumentException("容量错误!");
}
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 / 4 && data.length / 2 != 0){
resize(data.length / 2);
}
return ret;
}

/**
* 删除第一个
* @return
*/
public E removeFirst() {
return remove(0);
}

/**
* 删除最后一个
* @return
*/
public E removeLast() {
return remove(size - 1);
}

/**
* 查找某一元素并删除
* @param e
* @return
*/
public void removeElement(E e) {
int index = find(e);
if(index != -1){
remove(index);
}
}

/**
* 遍历数组
* @return
*/
@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append(String.format("Array: size = %d, capacity = %d\n", size, data.length));
res.append("[");
for(int i = 0; i < size; i++){
res.append(data[i]);
if(i != size-1){
res.append(", ");
}

}
res.append("]");
return res.toString();
}
}

数组队列的实现

// 基本接口定义
public interface Queue<E> {

/**
* 入队
* @param e
*/
void enqueue(E e);

/**
* 出队
* @return
*/
E dequeue();

/**
* 获取队头元素
* @return
*/
E getFront();

/**
* 获取队列元素
* @return
*/
int getSize();

/**
* 判断队列是否为空
* @return
*/
boolean isEmpty();

}

// 数组队列的实现
public class ArrayQueue<E> implements Queue<E> {

private Array<E> array;

public ArrayQueue(int capacity){
array = new Array<>(capacity);
}

public ArrayQueue() {
array = new Array<>();
}

public int getCapacity(){
return array.getCapacity();
}


@Override
public void enqueue(E e) {
array.addLast(e);
}

@Override
public E dequeue() {
return array.removeFirst();
}

@Override
public E getFront() {
return array.getFirst();
}

@Override
public int getSize() {
return array.getSize();
}

@Override
public boolean isEmpty() {
return array.isEmpty();
}

@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append("Queue : ");
res.append("front [");
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 class Main {

public static void main(String[] args) {
ArrayQueue<Integer> queue = new ArrayQueue<>();
for(int i = 0; i < 10; i++) {
queue.enqueue(i);
System.out.println(queue);
if(i % 3 == 2){
queue.dequeue();
System.out.println(queue);
}
}
}
}

循环队列的实现

public class LoopQueue<E> implements Queue<E> {

private E[] data;
private int front, tail;
private int size;

private void resize(int newCapacity) {
E[] newData = (E[])new Object[newCapacity + 1];
for(int i = 0; i < size; i++) {
newData[i] = data[(i + front) % data.length];
}
data = newData;
front = 0;
tail = size;
}

public LoopQueue(int capacity) {
data = (E[])new Object[capacity + 1];
front = 0;
tail = 0;
size = 0;
}

public LoopQueue() {
this(10);
}

public int getCapacity(){
return data.length - 1;
}


@Override
public void enqueue(E e) {
if((tail + 1) % data.length == front) {
resize(getCapacity() * 2);
}
data[tail] = e;
tail = (tail + 1) % data.length;
size++;
}


@Override
public E dequeue() {
if(isEmpty()) {
throw new IllegalArgumentException("队列为空!");
}
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;
}

@Override
public E getFront() {
if(isEmpty()) {
throw new IllegalArgumentException("队列为空!");
}
return data[front];
}

@Override
public int getSize() {
return size;
}

@Override
public boolean isEmpty() {
return front == tail;
}

@Override
public String toString() {

StringBuilder res = new StringBuilder();
res.append("Queue : ");
res.append("front [");
for(int i = front; i != tail; i = (i + 1)%data.length){
res.append(data[i]);
if((i + 1) % data.length != tail){
res.append(", ");
}
}
res.append("] tail");
return res.toString();

}
}
// 测试
public class Main {

public static void main(String[] args) {
LoopQueue<Integer> queue = new LoopQueue<>();
for(int i = 0; i < 10; i++) {
queue.enqueue(i);
System.out.println(queue);
if(i % 3 == 2){
queue.dequeue();
System.out.println(queue);
}
}
}
}