首页 文章

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

提问于
浏览
0

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

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

    rear.setNext(tmp);
    

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

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

    在此队列中,front = 1 rear = 4

    enqueue(5)
    

    这导致tmp = 5

    rear.setNext(5) 结果

    1,2,3,4,5

    rear=tmp 结果后面= 5将后指针重置为最后一个节点

  • 0

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

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

  • 1

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

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

    5 < - 4 < - 3 < - 2 < - 1 :: :-)(我该如何帮助你?)

    一个新人到达:6

    6 < - 5 < - 4 < - 3 < - 2 < - 1 :: :-)(我该如何帮助你?)

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

    6 < - 5 < - 4 < - 3 < - 2::-)(我该如何帮助你?)

相关问题