数据结构——队列的java实现(二)-循环队列
程序员文章站
2024-02-11 20:01:52
...
线性表有顺序存储结构
与链式存储结构
,栈是一种特殊的线性表
,所以有两种数据的存储方式。同样,队列作为一种特殊的线性表
,也同样存在两种数据的存储方式。
定义:我们把队列这种头尾相接的顺序存储结构称为循环队列。
数组队列存在的问题
如果前面节点删除了数据
,就会存在未被使用的数组空间
,则造成空间浪费
循环队列:
初始时front==tail
队列为空
如果尾指针
达到数组空间的最后一位
,这时有新的数据入队
,尾指针
则会移动到数组空间的第一位
,将数据放入0位
当新数据
放入0后,数组队列就满了
,这时我们有意识的浪费一个空间,用来判断是否满了
代码实现:
Queue.class
package code.ArrayCode;
public interface Queue<E> {
int getSize();
boolean isEmpty();
void enqueue(E e);
E dequeue();
E getFront();
}
LoopQueue.class
package code.ArrayCode;
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];
front = 0;
tail = 0;
size = 0;
}
//要维护队列,所以要浪费一个空间,当要获取capacity长度的时候要减去那个浪费的空间
public int getCapacity(){
return data.length-1;
}
public LoopQueue(){
this(10);
}
public int getSize() {
return size;
}
public boolean isEmpty() {
return front == tail;
}
public void enqueue(E e) {
if((tail + 1) % data.length == front){
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+front) % data.length];
}
data = newData;
front = 0;
tail = size;
}
public E dequeue() {
if(isEmpty()){
throw new IllegalArgumentException("Cannot dequeue from an empty queue.");
}
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()){
throw new IllegalArgumentException("Queue is empty.");
}
return data[front];
}
@Override
public String toString(){
StringBuilder res = new StringBuilder();
res.append(String.format("Queue: size = %d , capacity = %d\n" , size, getCapacity()));
res.append("front [");
//从front开始遍历,当取到tail时说明已经到最后一位了
//(i+1) % data.length每次加一,当分子小于分母的时候,直接取分子的数
//所以当要越界的时候就会取余到下一位
for(int i =front ; i != tail ; i = (i+1) % data.length){
res.append(data[i]);
//如果当前size不是最后一个,则添加一个,
if((i+1) % data.length != tail){
res.append(", ");
}
}
res.append("] tail");
return res.toString();
}
}
Main.class
public class Main {
public static void main(String[] args) {
LoopQueue<Integer> queue = new LoopQueue<Integer>();
for(int i = 0 ; i < 10 ; i++){
queue.enqueue(i);
System.out.println(queue);
if(i % 3 == 2){
queue.dequeue();
System.out.println(queue);
}
}
}
}
测试结果
Queue: size = 1 , capacity = 10
front [0] tail
Queue: size = 2 , capacity = 10
front [0, 1] tail
Queue: size = 3 , capacity = 10
front [0, 1, 2] tail
Queue: size = 2 , capacity = 5
front [1, 2] tail
Queue: size = 3 , capacity = 5
front [1, 2, 3] tail
Queue: size = 4 , capacity = 5
front [1, 2, 3, 4] tail
Queue: size = 5 , capacity = 5
front [1, 2, 3, 4, 5] tail
Queue: size = 4 , capacity = 5
front [2, 3, 4, 5] tail
Queue: size = 5 , capacity = 5
front [2, 3, 4, 5, 6] tail
Queue: size = 6 , capacity = 10
front [2, 3, 4, 5, 6, 7] tail
Queue: size = 7 , capacity = 10
front [2, 3, 4, 5, 6, 7, 8] tail
Queue: size = 6 , capacity = 10
front [3, 4, 5, 6, 7, 8] tail
Queue: size = 7 , capacity = 10
front [3, 4, 5, 6, 7, 8, 9] tail