?? queue.java
字號:
package com.lzw;
/**
“先進先出”(LIFO)的存儲結構。數據元素只能從隊尾進入,從隊首取出。在隊列中,
數據元素可以任意增減,但數據元素的次序不會改變。每當有數據元素從隊列中被取出,
后面的數據元素依次向前移動一位。所以,任何時候從隊列中讀到的都是隊首的數據。
根據這些特點,對隊列定義了以下六種操作:
push(x) 向隊列插入一個值為x的元素;
pop() 從隊列中取出一個元素;
front() 從隊列中讀一個元素,但隊列保持不變;
empty() 判斷隊列是否為空,空則返回真;
clear() 清空隊列;
search(x) 查找距隊首最近的元素的位置,若不存在,返回-1
*/
public class Queue extends java.util.Vector {
public class EmptyQueueException extends java.lang.RuntimeException {
public EmptyQueueException() {
super();
}
}
public Queue() {
super();
}
public synchronized void push(Object x) {
super.addElement(x);
}
public synchronized Object pop() {
/* 隊列若為空,引發EmptyQueueException異常 */
if (this.empty())
throw new EmptyQueueException();
Object x = super.elementAt(0);
super.removeElementAt(0);
return x;
}
public synchronized Object front() {
if (this.empty())
throw new EmptyQueueException();
return super.elementAt(0);
}
public boolean empty() {
return this.isEmpty();
}
public synchronized void clear() {
super.removeAllElements();
}
public int search(Object x) {
return super.indexOf(x);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -