初始数据结构——循环队列(顺序存储)

队列是只允许在队尾插入,队头删除的受限制的线性表
因为普通队列会出现假溢出现象,所以一般使用循环队列

public class CircularQueue
{
	private Object[] data;
    private int head;
    private int tail;
    private int size;

   //初始化队列
    public CircularQueue(int size) {
        data = new Object[size];
        head = -1;
        tail = -1;
        this.size = size;
    }
    
    //入队
    public boolean enQueue(Object value) {
        if (isFull() == true) {
            return false;
        }
        if (isEmpty() == true) {
            head = 0;
        }
        tail = (tail + 1) % size;
        data[tail] = value;
        return true;
    }
    
    //出队
    public boolean deQueue() {
        if (isEmpty() == true) {
            return false;
        }
        if (head == tail) {
            head = -1;
            tail = -1;
            return true;
        }
        head = (head + 1) % size;
        return true;
    }
    
  //取队头
    public Object Front() {
        if (isEmpty() == true) {
            return -1;
        }
        return data[head];
    }
    
   //取队尾
    public Object Rear() {
        if (isEmpty() == true) {
            return -1;
        }
        return data[tail];
    }
    
   //判空
    public boolean isEmpty() {
        return head == -1;
    }
    
  //判满
    public boolean isFull() {
        return ((tail + 1) % size) == head;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43574957/article/details/84988728