单向队列(循环队列)

package queue;

/**
 * 实现自己的队列
 */
public class QueueTest1 {
    public static void main(String[] args) {
        MyQueue myQueue = new MyQueue(3);
        myQueue.insert(1);
        myQueue.insert(2);
        myQueue.insert(3);

        System.out.println(myQueue.peek());

        while (!myQueue.isEmpty()) {
            System.out.print(myQueue.remove());
        }
    }
}

class MyQueue{
    private Object[] elemData;
    private int maxSize;   //数组最大长度
    private int rear;      //队尾指针
    private int front;     //队头指针
    private int nElems;    //当前元素个数

    //初始化
    public MyQueue(int size) {
        this.maxSize = size;
        elemData = new Object[size];
        rear = -1;
        front = 0;
        nElems = 0;
    }

    //插入数据元素
    public void insert(Object data) {
        //判断队满与否
        if (nElems == maxSize) {
            System.out.println("队列已满,新的数据项无法插入");
        } else {
            if ((rear + 1) == maxSize) rear = -1;
            elemData[++rear] = data;
            nElems++;
        }
    }

    //查看队列队头的数据项
    public Object peek() {
        //判断队列是否为空
        if (nElems == 0) {
            return null;
        }
        return elemData[front];
    }

    //出队
    public Object remove() {
        Object removeObj = null;
        if (nElems != 0) {
            removeObj = elemData[front];
            elemData[front] = null;
            nElems--;
            if (front == (maxSize - 1)) {
                front = 0;
            } else {
                front++;
            }
        }
        return removeObj;
    }

    //判断队列是不是空队列
    public boolean isEmpty() {
        return (nElems == 0);
    }

    //判断队列是不是满的
    public boolean isFull() {
        return (nElems == maxSize);
    }

    //返回队列大小
    public int getSize() {
        return nElems;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值