【问题标题】:Understanding the enqueue method for Linked List Queue了解链表队列的入队方法
【发布时间】:2013-12-02 00:23:21
【问题描述】:

我无法理解链表队列的入队方法的代码。我理解 dequeue()、isEmpty()、First() 和 size()。首先这里有 LinearNode 类来创建新的节点对象:

public class LinearNode<T> {
    private T element;
    private LinearNode<T> next;

    /**
     * Constructor: Creates an empty node.
     */
    public LinearNode() {
        next = null;
        element = null;
    }

    /**
     * Constructor: Creates a node storing the specified element.
     * @param elem  The specified element that is to be kept by this LinearNode.
     */
    public LinearNode(T elem) {
        next = null;
        element = elem;
    }

    /**
     *  Returns the node that follows this one.
     * @return  The node that follows this one.
     */
    public LinearNode<T> getNext() {
        return next;
    }

    /**
     *  Sets the node that follows this one.
     * @param node  The node which is to follow this one.
     */
    public void setNext(LinearNode<T> node) {
        next = node;
    }

    /**
     *  Returns the element stored in this node.
     * @return  The element that is kept within this node.
     */
    public T getElement() {
        return element;
    }

    /**
     *  Sets the element stored in this node.
     * @param elem  The element that is to be kept within this node.
     */
    public void setElement(T elem) {
        element = elem;
    }
}

这里是入队方法

public void enqueue(T element) {
    LinearNode<T> tmp = new LinearNode<T>(element);
    if (isEmpty()) {
        // set-up front to point to the new node
        front = tmp;
    } else {
        // add the node after the old tail node
        rear.setNext(tmp);
    }
    // update rear to point to the new node
    rear = tmp;
    count++; // increment size
}

我感到困惑的代码部分是rear.setNext(tmp);我的意思是不应该是temp.setNext(rear);你如何使用方法.setNext();在LinearNode&lt;T&gt; rear; 上,当您没有创建名为rear 的新对象时,我能看到的唯一新对象名为temp ??

编辑 这里是包含 Enqueue 方法的 LinkQueue 类:

public class LinkedQueue<T> implements QueueADT<T> {
private LinearNode<T> front; // front node of the queue
private LinearNode<T> rear;  // rear node of the queue
private int count;           // the current size of the queue

/**
 * Constructor: Creates an empty Queue.
 */
public LinkedQueue() { 
    count = 0;
    /* the following assignments are not actually necessary as references
     *  are initialised automatically to null, 
     *  but they are included for clarity.
     */
    front = null;
    rear = null;
}

【问题讨论】:

  • 谁和在哪里是前面和后面?
  • 刚刚编辑的帖子看看

标签: java linked-list queue


【解决方案1】:

从代码中我可以理解,前后端只是简单的指针。它们用于指向队列的第一个和最后一个节点。所以当你说:

rear.setNext(tmp);

您在队列的最后一个节点之后标记新节点。

考虑这个队列:1,2,3,4

在这个队列中, 前面=1 后方=4

enqueue(5)

这导致 tmp=5

rear.setNext(5) 结果

1,2,3,4,5

rear=tmp 导致 back=5 将尾部指针重置为最后一个节点

【讨论】:

    【解决方案2】:

    如果LinkedQueue&lt;T&gt; 为空tmp 将同时是列表的头部和尾部。 fronttail 两个指针都将链接到新节点。

    否则:rear 指向存储在尾部的对象。执行rear.setNext(tmp); 只会为该对象设置一个新的后续节点tmp。然后用正确的新对象rear = tmp 更新rear 引用。

    【讨论】:

      【解决方案3】:

      问题可能是阙中的链接方向。

      这是一个阙,有 5 个人在排队等候助理。它们向助手移动,但它们的链接方向相反:

      5

      一个新人到达:6

      6

      第一人完成:(他/她一直是先进的,这就是为什么他/她也是先出:FIFO)

      6

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-04
        • 1970-01-01
        • 2018-03-16
        • 2018-03-17
        相关资源
        最近更新 更多