引導語;Java是一種可以撰寫跨平臺應用程式的面向物件的程式設計語言。以下是本站小編分享給大家的.用Java編寫一個佇列類,歡迎閱讀!
根據這些特點,對佇列定義了以下六種操作:
enq(x) 向佇列插入一個值為x的元素;
deq() 從佇列刪除一個元素;
front() 從佇列中讀一個元素,但佇列保持不變;
empty() 判斷佇列是否為空,空則返回真;
clear() 清空佇列;
search(x) 查詢距隊首最近的元素的位置,若不存在,返回-1。
Vector類是JAVA中專門負責處理物件元素有序儲存和任意增刪的類,因此,用Vector
可以快速實現JAVA的佇列類。
public class Queue extends java
public synchronized void enq(ob ject x) {
lement(x);
}
public synchronized ob ject deq() {
/* 佇列若為空,引發EmptyQueueException異常 */
if( y() )
throw new EmptyQueueException();
ob ject x = entAt(0);
veElementAt(0);
return x;
}
public synchronized ob ject front() {
if( y() )
throw new EmptyQueueException();
return entAt(0);
}
public boolean empty() {
return pty();
}
public synchronized void clear() {
veAllElements();
}
public int search(ob ject x) {
return xOf(x);
}
}
public class EmptyQueueException extends java
}