自定义队列和自定义循环队列

用链表自定义实现一个队列,包括入队,出队,弹出队首元素,队列是否为空功能

class Node<T>{
    public T val;
    public Node<T> next;
    public Node(T val){
        this.val = val;
    }
}
public class MyQueue<T>{
    public Node<T> first;//队头
    public Node<T> last;//队尾
    //入队
    public boolean offer(T val){
        Node<T> node = new Node<T>(val);
        if (this.first == null){
            this.first = node;
            this.last = node;
        }else{
            this.last.next = node;
            this.last = node;
        }
        return true;
    }
    //出队
    public T poll(){
        if (isEmpty()){
            throw new RuntimeException("队列为空");
        }
        T res = this.first.val;
        if (this.first.next==null){
            this.last = null;
        }
        this.first = this.first.next;
        return res;
    }
    //弹出队首元素
    public T  peek(){
        if (isEmpty()){
            throw new RuntimeException("队列为空");
        }

        return this.first.val;
    }
    //判断队列是否为空
    public boolean isEmpty(){
        if (this.last == this.first && this.first == null){
            return true;
        }
        return false;
    }
}

来看看测试:

public class TestDemo2 {
    public static void main(String[] args) {
        MyQueue<String> myQueue = new MyQueue<>();
        myQueue.offer("Java");
        myQueue.offer("C++");

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

在这里插入图片描述
符合预期结果。

用数组实现一个循环队列,包括入队,出队,拿到队首元素,拿到队尾元素,判断队列是否为空,是否为满功能。

public class MyCircularQueue<T> {
    private int front;//队头下标
    private int rear;//队尾下标
    private T[] elem;
    public MyCircularQueue(int k){   //k代表存入元素的个数
        this.elem = (T[])new Object[k+1];  //这里初始化的时候一定要创建k+1大小的数组,因为会牺牲一个位置来判断队列是否为满
        this.front = 0;
        this.rear = 0;
    }
    //入队
    public boolean enQueue(T val){
        if (isFull()){
            return false;
        }
        this.elem[this.rear] = val;
        this.rear = (this.rear+1)%this.elem.length;
        return true;
    }
    //出队
    public boolean deQueue(){
        if (isEmpty()){
            return false;
        }
        this.front = (this.front+1)%this.elem.length;
        return true;
    }
    //拿到队首元素
    public T Front(){
        if (isEmpty()){
            throw new RuntimeException("队列为空");
        }
        T res = this.elem[this.front];
        return res;
    }
    //拿到队尾元素
    public T Rear(){
        if (isEmpty()){
            throw new RuntimeException("队列为空");
        }
        int index = 0; //定义一个下标,用来判断不同情况
        if (this.rear == 0){
            index = this.elem.length-1;
        }else{
            index = this.rear-1;
        }
        return this.elem[index];
    }
    //判断队列是否为满
    public boolean isFull(){
        return (this.rear+1)%this.elem.length == this.front;
    }
    //判断队列是否为空
    public boolean isEmpty(){
        return this.front == this.rear;
    }
}

来看看测试:

public class TestDemo3 {
    public static void main(String[] args) {
        MyCircularQueue<Integer> myCircularQueue = new MyCircularQueue<>(3);
        myCircularQueue.enQueue(1);
        myCircularQueue.enQueue(2);
        myCircularQueue.enQueue(3);
        System.out.println(myCircularQueue.Front());
        System.out.println(myCircularQueue.Rear());
        myCircularQueue.deQueue();
        myCircularQueue.deQueue();
        myCircularQueue.deQueue();
        System.out.println(myCircularQueue.Front());
    }
}

在这里插入图片描述
符合预期结果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宋丹尼尔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值