```
// "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());
}
}
}
```