Java数据结构:队列

导读:
数据结构示意图↓
image


image


image

public class queue<T> {
    private int maxSize; //队列初始化大小
    private Object[] arr; //定义数组
    private int front = 0; //队头指针
    private int rear = 0; //队尾指针

    /** 
     * 初始化指定大小队
     */ 
    public queue(int size) {
        this.maxSize = size;
        this.arr = new Object[maxSize];
    }

    /** 
     * 入队
     */ 
    public void offer(T t) {
        this.arr[rear++] = t;
    }

    /** 
     * 出队
     */ 
    public void poll() {
        Object tmp = arr[front];
        arr[front++] = null;
    }


    public int getMaxSize() {
        return maxSize;
    }

    public int getFront() {
        return front;
    }

    public int getRear() {
        return rear;
    }
}
public class queueT {
    public static void main(String[] args) {
        queue<Integer> qe = new queue(5);
        qe.offer(101);
        qe.offer(101);
        qe.offer(101);
        System.out.println(qe.getRear() - qe.getFront());
        qe.poll();
        System.out.println(qe.getRear() - qe.getFront());

    }
}

运行结果如下:
image

参考文章:

  1. 力扣图解算法数据结构 https://leetcode.cn/leetbook/read/illustration-of-algorithm/50e446/
  2. 顺序队列及C语言实现 http://c.biancheng.net/view/3353.html
posted @ 2022-10-18 21:29  Little_Monster-lhq  阅读(21)  评论(0编辑  收藏  举报