私はJavaの実装でキューについて読んでいました。キューのデキューとエンキューのメソッドを実装
public String dequeue() {
if (isEmpty()) {
throw new RuntimeException("Queue underflow");
} else if (first = last) {
String f = first.item;
first = null;
last = null;
return f;
}
String f = first.item;
first = first.next;
return f;
}
public void enqueue(String item) {
Node x = new Node(item);
if (first = last = null) {
first = last = x;
}
last.next = x;
last = x;
}
は私がデキューに右やっている()およびエンキュー()メソッド:私は、次のコード
public class QueueOfStrings {
private Node first = null; // least-recently added
private Node last = null; // most-recently added
private class Node {
private String item;
private Node next;
}
// is the queue empty?
public boolean isEmpty() {
return first == null;
}
public String dequeue() {
if (isEmpty()) {
throw new RuntimeException("Queue underflow");
}
String item = first.item;
first = first.next;
return item;
}
public void enqueue(String item) {
Node x = new Node();
x.item = item;
if (isEmpty()) {
first = x;
last = x;
} else {
last.next = x;
last = x;
}
}
私はこのような私の方法でそれらを書き換えたのを発見しましたか? mainメソッドで
私は次のように実行する必要があります。
public static void main(String[] args) {
QueueOfStrings q = new QueueOfStrings();
q.enqueue("roro");
q.enqueue("didi");
q.enqueue("lala");
System.out.println(q.dequeue());
}
おかげで何を意味するか
「私はデキューでの書き込み()およびエンキュー()メソッドをやっていますか?」 ?また、あなたの最後のものはif(first == null && last == null){if(first = last = null)の代わりに{ – matt
} null ' – Joe
これはコンパイルされません。 first = last = nullで何をしようとしていますか? –