简体   繁体   English

LinkedList 临时头更新原始头

[英]LinkedList temporary head updating the original head

I'm trying to implement a LinkedList in java .我正在尝试在java实现LinkedList Currently, LinkedList contains only 2 methods insertAtFront and insertAtBack which is working fine.目前, LinkedList只包含两种工作正常的方法insertAtFrontinsertAtBack But I want to clear my concept about when we assign a new Node to the temporary head it is updating the original head at the following line.但我想澄清我的概念,当我们将新Node分配给临时head它会在下一行更新原始head

headTemp.reference = new Node(data); headTemp.reference = 新节点(数据);

How it is happening?它是如何发生的? Because by default an object is a reference type.因为默认情况下,对象是引用类型。 Please explain this concept in plain English.请用简单的英语解释这个概念。

Node class:节点类:

public class Node {

    public int data;
    public Node reference;

    public Node(int data) {
        // Calling the second constructor
        this(data, null);
    }

    public Node(int data, Node reference) {
        this.data = data;
        this.reference = reference;
    }

    public int getData() {
        return this.data;
    }

    public Node getNextNode() {
        return this.reference;
    }

}

LinkedList class:链表类:

public class LinkedList {

    // Head
    protected Node head;

    // Default head would be null
    public LinkedList() {
        this.head = null;
    }

    // insertAtFront first check if head is empty
    public void insertAtFront(int data) {
        if (isEmpty()) {
            head = new Node(data);
        } else {
            head = new Node(data, head);
        }

    }

    // insertAtBack
    public void insertAtBack(int data) {
        if (isEmpty()) {
            head = new Node(data);
        }
        // If List has second node
        else if (head.reference == null) {
            head.reference = new Node(data);
        } else {
            Node headTemp = head;

            while (headTemp.reference != null) {
                headTemp = headTemp.reference;
            }
            headTemp.reference = new Node(data);
        }
    }

    public boolean isEmpty() {
        return this.head == null;
    }

    public void printList() {
        System.out.printf("Linked list is : ");

        Node headTemp = head;

        while (headTemp != null) {

            System.out.printf(" %s ", headTemp.data);

            headTemp = headTemp.reference;
        }

        System.out.printf("\n");
    }

}

Main class:主要类:

public class Main {

    public static void main(String[] args) {

        LinkedList l = new LinkedList();

        l.insertAtBack(23);
        l.insertAtBack(24);
        l.insertAtBack(25);

        l.printList();

    }

}

Java variables hold as value (a reference to) the object, the Node instance. Java 变量作为值(引用)对象,即 Node 实例。 Assignment will share the object.赋值将共享对象。

Hence changes to the object can be reflected in several variables/fields.因此,对对象的更改可以反映在多个变量/字段中。

Comments on the code, which is very fine on the inserts:对代码的评论,在插入上非常好:

public void insertAtFront(int data) {
    head = new Node(data, head);
}

//public LinkedList() {
//    this.head = null;
//}

Fields are automatically null, 0, 0.0, false.字段自动为 null、0、0.0、false。

The class Node probably need not be public.类 Node 可能不需要是公开的。

From what i understood you do not understand the Logic of根据我的理解,你不理解的逻辑

else {
       Node headTemp = head;
       while (headTemp.reference != null) {
            headTemp = headTemp.reference;
       }
       headTemp.reference = new Node(data);
     }

First of all the headTemp is a confusing name for the variable , it should be something like tempPointer because what we are doing here is saying i have a pointer which is pointing to the address of a specific node of linked list.首先, headTemp 是变量的一个令人困惑的名称,它应该类似于 tempPointer 因为我们在这里所做的是说我有一个指向链表特定节点地址的指针。

Here the tempPointer is refering to the same variable as head这里的 tempPointer 指的是与 head 相同的变量

This is the same logic as这是相同的逻辑

String s= new String("ABC");
String s1=s;

so both s and s1 as an address of variable new String("ABC");所以 s 和 s1 作为变量 new String("ABC") 的地址;

So our position now is [Head/tempPointer]-> []-> []-> []-> []-> []所以我们现在的位置是 [Head/tempPointer]-> []-> []-> []-> []-> []

Then because we have address of head we can find address of its next node using tempPointer.reference(the next node address) and say ok i now have the next node address so i dont need the current node address so i will replace.然后因为我们有头部地址,我们可以使用 tempPointer.reference(下一个节点地址)找到它的下一个节点的地址,然后说好的,我现在有了下一个节点地址,所以我不需要当前节点地址,所以我将替换。

tempPointer = tempPointer.reference; tempPointer = tempPointer.reference;

Now it will look like [Head]-> [tempPointer]-> []-> []-> []-> []现在它看起来像 [Head]-> [tempPointer]-> []-> []-> []-> []

we do this in a loop until there is no more next node "tempPointer.reference = null" , which is the end of the list.我们在循环中执行此操作,直到没有下一个节点 "tempPointer.reference = null" ,这是列表的末尾。

[Head]-> []-> []-> []-> []-> [tempPointer] [头部]-> []-> []-> []-> []-> [tempPointer]

At this point my tempPointer has address of last node so if i add the Node to the end of this address , i will add the node to the linked list此时我的 tempPointer 具有最后一个节点的地址,因此如果我将节点添加到该地址的末尾,我会将节点添加到链表中

tempPointer.reference = new Node(data); tempPointer.reference = 新节点(数据);

[Head]-> []-> []-> []-> []-> [tempPointer]=> [Added Node(new Node())] [Head]-> []-> []-> []-> []-> [tempPointer]=> [Added Node(new Node())]

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

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