私はリンクリストを含む宿題に取り組んでいます。キューADTを実装する必要がありますが、私が問題を抱えている1つの方法は、リストの最後にノードを追加することです(enqueueメソッド)。これが私のコードです:
パブリッククラスQueueはQueueInterfaceを実装します{
private Node head;
private Node tail;
private int sz;
public Queue() {
head = null;
tail = null;
sz = 0;
}
public void enqueue(T newEntry) {
Node newElement = new Node(newEntry);
newElement.next = null;
tail.next = newElement;
tail = newElement;
}
public T dequeue() {
T result = null;
if(head != null) {
result = head.data;
head = head.next;
sz--;
}
return result;
}
public T getFront() {
return head.data;
}
public boolean isEmpty() {
if (head == null) {
return true;
}
return false;
}
public void clear() {
while (!isEmpty()) {
dequeue();
}
}
@Override
public String toString() {
return "Queue [head=" + head + ", sz=" + sz + "]";
}
public class Node {
private Node next;
private T data;
public Node (T newData) {
data = newData;
}
@Override
public String toString() {
return "Node [next=" + next + ", data=" + data + "]";
}
}
}
誰かがこれで私を手伝ってくれるなら、私は本当に感謝するでしょう。御時間ありがとうございます!:)