0
我只是好奇,我有一個類實現了一個隊列,它目前有一個項目...我很好奇,哪裏將是最有效或最乾淨的位置來初始化其他項目,我希望添加到隊列中......這是作業的一部分,所以請提供解釋,而不僅僅是回答!在此先感謝...這是我建立了類...在Java中執行隊列
import java.util.*;
public class Queue<T> extends Node<T> {
private LinkedList<T> list;
// Queue constructor
public Queue() {
// Create a new LinkedList.
list = new LinkedList<T>();
}
//check if empty
public boolean isEmpty() {
return (list.size() == 0);
}
//add items to queue
public void enqueue(Object item) {
// Append the item to the end of our linked list.
list.add((T) item);
}
//remove items from queue
public T dequeue() {
T item = list.get(1);
list.remove(1);
// Return the item
return item;
}
//check top item
public T peek() {
return list.get(1);
}
//check size of queue
public int size() {
return list.size();
}
}
你對此事的看法? – 2013-03-04 17:27:23
爲什麼'Object'而不是'T'到處都是? – 2013-03-04 17:27:28
你確定你可以使用'LinkedList'來操縱你的隊列嗎?它基本上是一個包裝,可能你的教授不會接受它。 – 2013-03-04 17:28:09