簡體   English   中英

使用Java中的圓形鏈接隊列創建出隊方法

[英]Creating a dequeue method using a Circlular linked Queue in java

我在弄清楚如何完成我的出隊方法時遇到了麻煩。 我只允許使用后指針實例變量。 我從隊列中刪除了前兩個條目,但隨后它沒有刪除其余的條目。 我的代碼如下。 我非常困惑我的方法正在發生什么。 謝謝

public class CircularLinkedQueue<T> implements QueueInterface<T>
{
private Node lastNode;

@Override
public void enqueue(Object newEntry) 
{
    Node newNode = new Node(newEntry, null);
    if(lastNode == null)
        newNode.setNextNode(newNode);
    else
    {
        newNode.setNextNode(lastNode.getNextNode());
        lastNode.setNextNode(newNode);
    }
    lastNode = newNode;
}

@SuppressWarnings("unchecked")
@Override
public T dequeue() 
{
    T result = null;
    if(!isEmpty())
    {
        result = (T) lastNode.getNextNode().getData();
        lastNode = lastNode.getNextNode();

        if(lastNode.getNextNode() == null)
            lastNode = null;
        else
            lastNode.getNextNode().setNextNode(null);
    }
    return  result;
}

@Override
public T getFront() 
{
    T results = null;
    if(!isEmpty())
        results = (T) lastNode.getNextNode().getData();
    return results;
}

@Override
public boolean isEmpty() 
{
    if(lastNode == null)
        return true;
    else return false;
}

@Override
public void clear() 
{
    lastNode = null;
}

}

我的出隊驅動程序應該是這樣的。

    public static void main(String[] args) {

    System.out.println("Create a queue: ");
    QueueInterface<String> myQueue = new CircularLinkedQueue<String>();
    myQueue.enqueue("Ann");
    myQueue.enqueue("Bill");
    myQueue.enqueue("Carol");
    myQueue.enqueue("David");
    myQueue.enqueue("Edgar");
    myQueue.enqueue("Fred");

    while (!myQueue.isEmpty()) {
        Object front = myQueue.getFront();
        System.out.println("\t" + front + " is at the front of the queue.");

        front = myQueue.dequeue();
        System.out.println("\t" + front + " is removed from the front of the queue.");
    } 
}  
}

輸出應如下所示

Ann is at the front of the queue.

Ann is removed from the front of the queue.

Bill is at the front of the queue.

Bill is removed from the front of the queue.

Carol is at the front of the queue.

Carol is removed from the front of the queue.

David is at the front of the queue.

David is removed from the front of the queue.

Edgar is at the front of the queue.

Edgar is removed from the front of the queue.
Fred is at the front of the queue.

Fred is removed from the front of the queue.

我的輸出看起來像這樣

Ann is removed from the front of the queue.

Bill is at the front of the queue.

Bill is removed from the front of the queue.

您的出隊方法有問題。 (我認為)這應該是:

public T dequeue() {
    if (isEmpty()) {
        throw new NoSuchElementException("The queue is empty!");
    }

    T result = lastNode.getNextNode().getData();

    if (lastNode.getNextNode() == lastNode) {
        lastNode = null;
    } else {
        lastNode.setNextNode(lastNode.getNextNode().getNextNode()); 
    }
    return result;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM