【问题标题】:Creating a dequeue method using a Circlular linked Queue in java在java中使用循环链接队列创建出队方法
【发布时间】:2014-11-11 05:46:06
【问题描述】:

我无法弄清楚如何完成我的出队方法。我只允许使用后指针实例变量。我正在从队列中删除前两个条目,但它不会删除其余条目。我的代码如下。我非常困惑我的方法发生了什么。谢谢

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.

【问题讨论】:

    标签: java linked-list circular-list


    【解决方案1】:

    您的出队方法有问题。这是(我认为)应该是的:

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-05
      • 2016-02-16
      • 2020-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多