Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java双链表最后一个节点赢了';不要正确地指向列表_Java - Fatal编程技术网

Java双链表最后一个节点赢了';不要正确地指向列表

Java双链表最后一个节点赢了';不要正确地指向列表,java,Java,我想在列表的末尾添加一个节点,但我似乎无法连接第一个节点与其结束位置之间的链接 我知道我的逻辑不可能是正确的,因为我能让它工作的唯一方法是覆盖最后一个并将其赋值为null。如果我不这样做,程序就会进入无限循环,因为我猜没有一个节点=null来阻止循环结束。我想第20行将创建一个新对象来保持null,然后第21行将temp链接回它 public class LinkedListDeque { public DoubleNode first = new DoubleNode(); public D

我想在列表的末尾添加一个节点,但我似乎无法连接第一个节点与其结束位置之间的链接

我知道我的逻辑不可能是正确的,因为我能让它工作的唯一方法是覆盖最后一个并将其赋值为null。如果我不这样做,程序就会进入无限循环,因为我猜没有一个节点=null来阻止循环结束。我想第20行将创建一个新对象来保持null,然后第21行将temp链接回它

public class LinkedListDeque {

public DoubleNode first = new DoubleNode();
public DoubleNode last = new DoubleNode();
public DoubleNode temp;
public int N;


private class DoubleNode {

    String item;
    int counter = 0;
    DoubleNode next;
    DoubleNode prev;
    DoubleNode last;

    DoubleNode() {
    }

    DoubleNode(String i) {
        this.item = i;
    }

}


public void enqueue(String item) {

    temp = getNode(null); //returns pointer to last node in "first"

    System.out.println("\nenqueue\n***********");

    DoubleNode oldlast;        

    oldlast = temp;
    temp.item = item;

    last = temp;

    System.out.println("last = " + last.item);  // = last item
    System.out.println("temp = " + temp.item);  // = last item    

line 20     temp = new DoubleNode();    
line 21     temp = oldlast;    

    DoubleNode last;              //will go into infinite loop w/out 
    last = temp;                  //these two lines

    System.out.println("last = " + last.item);   // = null
    System.out.println("temp = " + temp.item);   // = null

    if (isEmpty()) {            //returns true if first == null
        first = last;
    } else {
        oldlast.next = last;

    }
    N++;
 }

}

我认为您的实现可以如下所示:

public final class LinkedListDeque {

    private Node root;
    private int size;

    public void enqueue(String val) {
        Node node = new Node(val);

        if (size == 0) {
            node.next = node;
            node.prev = node;
            root = node;
        } else {
            Node last = root.prev;

            last.next = node;
            node.prev = last;
            node.next = root;
            root.prev = node;
        }

        size++;
    }

    public String dequeue() {
        if (size == 0)
            throw new NoSuchElementException();

        String val = root.val;

        if (size == 1) {
            root.next = null;
            root.prev = null;
            root = null;
        } else {
            Node head = root.next;

            root.prev.next = head;
            head.prev = root.prev;
            root.next = null;
            root.prev = null;
            root = head;
        }

        size--;
        return val;
    }

    private static class Node {

        private final String val;
        private Node next;
        private Node prev;

        public Node(String val) {
            this.val = val;
        }
    }

}

如果您的LinkedList正确地维护了第一个和最后一个节点,那么您不需要为了将新节点放在末尾而进行任何遍历。只需将当前最后一个设置为新节点旁边,将新节点设置为当前最后一个之前,然后将当前最后一个替换为新节点

当您退出队列时,只需确保清除first和last if
last.getNext()==null
,以便下一个队列正确设置first和last

public class LinkedListDequeue<T> {
    private DoubleNode first;   // First node
    private DoubleNode last;    // Last node
    private size = 0;

    public static class DoubleNode<T> {
       private T value;
       private DoubleNode prev;
       private DoubleNode next;

       ...
    }

    public DoubleNode enqueue(DoubleNode<T> node) {
        if (first == null) {
            // If empty set node to the first and last node
            first = last = node;
        } else {
            // Set the current last node next -> new node and then the
            // new node previous to the last and then set the new node
            // as last.
            last.setNext(node);
            node.setPrev(last);             
            last = node;    
        }

        size++;
        return node;
    }

    public DoubleNode enqueue(T value) {
        return enqueue(new DoubleNode(value));
    }
}
公共类LinkedListDequeue{
private DoubleNode first;//第一个节点
私有DoubleNode last;//最后一个节点
私人规模=0;
公共静态类双节点{
私人T值;
私有双节点prev;
私有双节点下一步;
...
}
公共双节点排队(双节点){
if(first==null){
//如果为空,则将节点设置为第一个和最后一个节点
第一个=最后一个=节点;
}否则{
//设置当前最后一个节点next->new node,然后设置
//新建上一个节点,然后设置新节点
//最后一次。
last.setNext(节点);
node.setPrev(最后一个);
last=节点;
}
大小++;
返回节点;
}
公共双节点排队(T值){
返回排队(新的DoubleNode(值));
}
}
理论上,
通过循环列表获取最后一个节点
分配最后一个=最后一个节点
创建新对象()
分配新对象=last.next,它将新对象链接到原始列表并使其=null


但这实际上不起作用。奇怪的是,这个想法有多么简单,但现在却很难应用

一步一步地从头开始编写代码。首先设计算法,然后编写代码。因此,当您编写(node.prev=node)和(node.next=node)时,如果您刚在enqueue()的开头创建了一个新节点,node.next和node.prev是否会指向任何东西?@Travelsbyfire我只将节点添加到尾部。如果我将root替换为first,这是同一回事吗?我正在尝试将我的代码与您的代码相适应,但似乎无法正确使用。是的,您可以先用
重命名
root
prev
next
始终不为
null
。第一个元素的“上一个”是最后一个元素。最后一个元素的下一个元素是第一个元素。