java数组实现循环队列_java使用数组实现循环队列

class CircleArrayQueue {

private int size;//队列的长度

private int[] queue; //队列

private int head; //后指针

private int tail; //前指针

private static final int DEFALUT_SIZE = 10;

public CircleArrayQueue() {

this.size = DEFALUT_SIZE;

}

public CircleArrayQueue(int queueSize) {

if (queueSize <= 0 ) {

size = DEFALUT_SIZE;

queue = new int[DEFALUT_SIZE];

} else {

size = queueSize;

queue = new int[queueSize];

}

}

public boolean isFull() {

return (tail + 1) % size == head;

}

public boolean isEmpty() {

return tail == head;

}

public int count() {

return (tail + size - head) % size;

}

public void add(int num) {

if (isFull()) {

System.out.println("队列已满,不能再添加元素!!");

return;

}

queue[tail] = num;

tail = (tail + 1) % size;

}

public int get() {

if (isEmpty()) {

throw new RuntimeException("队列为空~~~");

}

int num = queue[head];

head = (head + 1) % size;

return num;

}

public int peek() {

if (isEmpty()) {

throw new RuntimeException("队列为空~~~");

}

return queue[head];

}

public void list() {

for (int i = head; i < head + count(); i++) {

System.out.printf("queue[%d] = %d,", i % size, queue[i % size]);

}

System.out.println();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值