企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
``` // "static void main" must be defined in a public class. class MyQueue { // 存放数据的数组 private List<Integer> data; // 表明队头元素位置,index=1,不光是队头元素为1,而且已经出队了一个元素 private int p_start; //队列初始化 public MyQueue() { data = new ArrayList<Integer>(); p_start = 0; } /** 从队尾插入一个元素,并且不需要队尾指针记录*/ public boolean enQueue(int x) { data.add(x); return true; }; /** 出队一个元素 */ public boolean deQueue() { //首先判断不为空 if (isEmpty() == true) { //队列空,出队失败 return false; } //出队一个元素,记录队头的元素位置的值加1 p_start++; return true; } /** 根据头指针索引得到队头元素*/ public int Front() { return data.get(p_start); } public boolean isEmpty() { //如果出队的元素大于等于队列中元素个数,则队列已经为空 return p_start >= data.size(); } }; public class Main { public static void main(String[] args) { MyQueue q = new MyQueue(); //添加元素 q.enQueue(5); q.enQueue(3); //队列不为空,打印队头位置 if (q.isEmpty() == false) { System.out.println(q.Front()); } //出队, q.deQueue(); //队列不为空,打印队头位置 if (q.isEmpty() == false) { System.out.println(q.Front()); } q.deQueue(); if (q.isEmpty() == false) { System.out.println(q.Front()); } } } ```