简体   繁体   English

仅具有一个前哨节点的双端队列的addLast方法

[英]addLast method for a deque with only one sentinel node

This question is from Berkeley's data structures free online course (CS61B) The link can be found here: https://joshhug.gitbooks.io/hug61b/content/chap2/chap23.html 这个问题来自伯克利的数据结构免费在线课程(CS61B)。可以在这里找到链接: https ://joshhug.gitbooks.io/hug61b/content/chap2/chap23.html

Implement the list so that it is circular, with the front and back pointers sharing the same sentinel node. 实现列表,使其为圆形,前后指针共享相同的前哨节点。 Add and remove operations must not involve any looping or recursion. 添加和删​​除操作不得涉及任何循环或递归。 A single such operation must take “constant time”, ie execution time should not depend on the size of the deque. 单个此类操作必须花费“恒定时间”,即执行时间不应取决于双端队列的大小。

[Box and Pointer Diagram for the allocated task] [1]: https://i.stack.imgur.com/pUgk3.png [已分配任务的方框图和指针图] [1]: https : //i.stack.imgur.com/pUgk3.png

Eg if my list is {1,2,3} then sentinel.next.next.item is 3, and sentinel.next.next.next.item is 1 例如,如果我的列表为{1,2,3},则sentinel.next.next.item为3,sentinel.next.next.next.item为1

 public class DLList<T> {

        private class Node {
            public T item;
            public Node next;
            public Node prev;

            public Node(T item, Node next, Node prev) {
                this.item = item;
                this.next = next;
                this.prev = prev;
            }

            @Override
            public String toString() {
                return item.toString();
            }
        }

        public Node sentinel ;
        private int size;

        public DLList() {
            this.sentinel = null;
            this.size = 0;
        }

        public void addLast(T item) {
            sentinel.next = new Node(item, null, sentinel);
            sentinel = sentinel.next;  // updatedSentinel
            size++;
        }
    }

I would just like to ask, how do I make sure the updatedSentinel.next links all the way back to the first node? 我想问一下,如何确保updatedSentinel.next一直链接到第一个节点? Also, is my constructor correct for the purposes of this class? 另外,对于此类而言,我的构造函数是否正确? Is the implementation supposed to be different when the size is 0 and when the size >= 1? 当大小为0且大小> = 1时,实现方式是否应该有所不同?

First,you have to check if the Linked List is empty or not.If it is empty,then link prev,next and sentinel to the current node. 首先,必须检查链接列表是否为空。如果为空,则将prev,next和哨兵链接到当前节点。

if(head == null)
{
  Node a = new Node(item,a,a);
  sentinel.next = a; 
}

Otherwise find the last node and assign its next node to the first node.Similarly,assign the prev node to the last node.You can keep track of sentinel's next node as head node. 否则,找到最后一个节点并将其下一个节点分配给第一个节点。类似地,将上一个节点分配给最后一个节点。您可以跟踪哨兵的下一个节点作为头节点。

Node head = sentinel.next;
Node last = head;
while(last.next != head)
{
   last = last.next;
}

Node a = new Node(item,head,last);
head.prev = a;
last.next = a;

I don't think there is any mistake with your constructor class. 我认为您的构造函数类没有任何错误。

Make the sentinel point to the last node: it will be null if the list is empty, otherwise, sentinel.next is the first node (because the list is circular). 将哨兵指向最后一个节点:如果列表为空,它将为null;否则,sentinel.next为第一个节点(因为该清单是循环的)。 You don't need any backward link. 您不需要任何反向链接。 So addLast would be: 所以addLast将是:

    public void addLast(T item) {
        if (sentinel == null) {
            sentinel = new Node(item, null);
            sentinel.next = sentinel;
        } else {
            sentinel.next = new Node(item, sentinel.nex);
            sentinel = sentinel.next;  // updatedSentinel
        }
        size++;
    }

¨ UPDATE: with both links: ¨更新:两个链接:

    public void addLast(T item) {
        if (sentinel == null) {
            sentinel = new Node(item, null, null);
            sentinel.next = sentinel;
            sentinel.prev = sentinet;
        } else {
            Node next = sentinel.next;
            sentinel.next = next.prev = new Node(item, next, sentinel);
            sentinel = sentinel.next;
        }
        size++;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM