了解链接列表队列的入队方法

时间:2013-11-18 18:13:06

标签: java linked-list queue

我无法理解链接列表队列的入队方法的代码。我理解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;
    }
}

这是Enqueue方法

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

3 个答案:

答案 0 :(得分:1)

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

rear.setNext(tmp);

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

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

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

enqueue(5)

这导致tmp = 5

rear.setNext(5)会产生

1,2,3,4,5

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

答案 1 :(得分:0)

如果LinkedQueue<T>为空tmp,则同时是列表的头尾。指针fronttail都将链接到新节点。

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

答案 2 :(得分:0)

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

这是一个排队等候助手的5人队。他们走向助手,但他们是相反的方向:

5 < - 4 < - 3 < - 2&lt; - 1:: - )(我该如何帮助您?)

一个新人到达:6

6 < - 5 < - 4 < - 3 < - 2&lt; - 1:: - )(我该如何为您提供帮助?)

第一个人完成了:(他/她是第一个进入,这也是他/她也是第一个出局的原因:FIFO)

6 < - 5 < - 4 < - 3&lt; - 2::-)(我该如何帮助您?)