简体   繁体   English

Java链表指针混乱

[英]Java Linked List pointer confusion

This Code below is from a java LinkedList implementation.The method adds a string element at an index point of the list and is taken from one of my cs books. 下面的代码来自Java LinkedList实现。该方法在列表的索引点添加了一个字符串元素,并取自我的一本CS书籍。

The linked list class has 2 global private variables 链表类有2个全局私有变量

Node first;
Node last;


public void add(int index, String e) {
    if (index < 0 || index > size()) {
        String message = String.valueOf(index);
        throw new IndexOutOfBoundsException(message);
    }

    // Index is at least 0
    if (index == 0) {
        // New element goes at beginning
        first = new Node(e, first);
        System.out.println("ran");
        if (last == null)
            last = first;
        return;
    }

    // Set a reference pred to point to the node that
    // will be the predecessor of the new node
    Node pred = first;
    for (int k = 1; k <= index - 1; k++) {
        pred = pred.next;
    }

    // Splice in a node containing the new element
    pred.next = new Node(e, pred.next);
    System.out.println(toString());

    // Is there a new last element ?
    if (pred.next.next == null)
        System.out.println("ran");
        last = pred.next;
}

My question 我的问题
I don't understand how Node first, last get updated in the condition below 我不明白如何在以下情况下Node first, last更新Node first, last更新Node first, last

Suppose you have a list that looks like ["1","2","3","7","4","5,"6"] 假设您的列表看起来像["1","2","3","7","4","5,"6"]

Then you add the the element "4" to index 3 然后将元素“ 4”添加到索引3

So, the list looks likes ["1","2","3","4","7","4","5,"6"] , but looking at the code of the add method I don't know how the first or last node pointers gets updated. Because in my mind these are the only pieces of code that run because the index isn't 0 and the last doesn't change 因此,列表看起来像["1","2","3","4","7","4","5,"6"] ,但是看一下我不喜欢的add方法的代码不知道第一个最后一个节点指针是如何更新的。因为在我看来,这是唯一运行的代码,因为索引不是0并且最后一个不变

EDIT 编辑

The Object Node first is used in the toString method(not shown) to traverse through the collection first在toString方法(未显示)中使用对象节点遍历集合

    // Set a reference pred to point to the node that
    // will be the predecessor of the new node
    Node pred = first;
    for (int k = 1; k <= index - 1; k++) {
        pred = pred.next;
    }
    // Splice in a node containing the new element
    pred.next = new Node(e, pred.next);
    System.out.println(toString());

Before the add, the first element is "1" and the last element is "6". 在添加之前,第一个元素为“ 1”,最后一个元素为“ 6”。
After the add, the first element is "1" and the last element is "6". 添加后,第一个元素为“ 1”,最后一个元素为“ 6”。
You're right that the first and last dont change-- they don't need to, because you haven't changed the first or the last element. 没错,第一个最后一个不需要更改,因为它们没有更改,因为您没有更改第一个或最后一个元素。

I think you are getting bogged down by the way this linked list implementation works. 我认为您对这种链表实现的工作方式感到困惑。 The first and last pointers exist to just keep track of the start and end of the list. 存在firstlast指针只是为了跟踪列表的开始和结束。 Hence, when you insert an element in the middle of the list, these pointers do not get updated, nor is there any need for such an update. 因此,当您在列表的中间插入元素时,这些指针不会更新,也不需要进行任何更新。 But they do get updated should an insert land before the current head or after the current tail of the list. 但是, 如果在当前列表的当前标题之前或之后,插入内容降落,它们的确会更新。 Here is the code which handles head inserts: 这是处理头部插入的代码:

if (index == 0) {
    // New element goes at beginning
    first = new Node(e, first);
    System.out.println("ran");
    if (last == null)
        last = first;
    return;
}

The critical line here is actually this: 关键线实际上是这样的:

first = new Node(e, first);

The first pointer gets assigned to a new node, which in turn points to the old first node. first指针被分配给一个新节点,该新节点又指向旧的 first节点。 Similarly, should an insert of a new node land at the end of the list, the following code will handle that: 同样,如果在列表的末尾插入新节点,则以下代码将处理该问题:

if (pred.next.next == null) {
    System.out.println("ran");
    last = pred.next;
}

Here the last pointer is assigned to the new node which was inserted after the old last . 在这里, last指针分配给在旧的last 之后插入的新节点。

But other than these two edge cases, there is no need to update first and last with inserts. 但是,除了这两种情况以外,不需要使用插入方法来first更新和last更新。

If the index is 0, the node is added in the beginnng and the for loop doesn't work. 如果索引为0,则将节点添加到beginnng中,并且for循环不起作用。 If it is 1, the loop again doesn't execute and node is simply added to the list. 如果为1,则不会再次执行循环,而只是将节点添加到列表中。 However if it something else, then until the index-1 position is reached the loop shifts each element one step behind. 但是,如果还有其他问题,则直到到达索引1的位置为止,循环才会将每个元素向后移动一步。 Then the code just outside the loop (which slices in a node containing new element) inserts the element at index. 然后,循环外的代码(在包含新元素的节点中进行切片)将元素插入到索引处。 If after the loop has executed, the index turns out to be the last one then the last node is updated. 如果执行完循环后,索引变成最后一个索引,则更新最后一个节点。

Hope this helps! 希望这可以帮助!

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

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