1
みなさん、こんにちは皆さんは、2つのポインタ(頭、尾)を持つjavaでfifoキューの実装を書いています。頭のポインタだけを使用するFIFOキューを作成する方法があるかどうかを知りたいと思います。1つのポインタを持つFIFOキュー
私のコードは次のとおりです:
import java.io.PrintStream;
import java.util.*
public class StringQueueImpl<T> implements StringQueue {
private int total; // number of elements on queue
private Node head; // beginning of queue
private Node tail; // end of queue
private class Node {
T ele;
Node next;
Node(T ele) {
this.ele = ele;
next = null; }
}
/**
* Creates an empty queue.
*/
public StringQueueImpl() {
first = null;
last = null;
total = 0;
}
boolean isEmpty() {
return (head == null);
}
public <T> void put(T ele) {
Node t = tail;
tail = new Node(ele);
if (isEmpty()) head = tail;
else t.next = tail;
total++;
}
public T get() {
if (isEmpty()) throw new NoSuchElementException();
T v = head.ele;
Node t = head.next;
head = t;
return v;
total--;
}
public T peek() {
if (isEmpty()) throw new NoSuchElementException();
return head.ele;
}
Node node = head;
public void printQueue(PrintStream stream){
while(node != null){
stream.println(node.ele);
stream.flush();
node = node.next;
}
}
public int size(){
return total;
}
}