为什么 sLL.node.value 不工作,但 sLL.head.value 在 Java 中只有一个节点的单链表中显示正确的输出?概念不清

Why is sLL.node.value not working but sLL.head.value shows correct output in singly linked list with only one node in Java? Unclear of concept

我是 java 的初学者,刚开始使用链表。我在 java 中编写了一个简单的程序,它创建了一个只有一个节点的链表,头和尾指向同一个节点。但是,我有一些疑问需要澄清,如果有人能解决这些问题,我将不胜感激。

代码如下:

  1. Node.java class
public class Node{
  public int value;//Declared value field for node 
  public Node next;//Declared link to next node in java
}
  1. SinglyLinkedList.java class

public class SinglyLinkedList{
  public  Node head;//DEclared head node
  public  Node tail;//Declared tail node

  public Node createsSinglyLinkedList(int inputvalue){
    head=new Node();//Memory allocation using new operator
    tail=new Node();//Memory allocation using new operator
    Node node=new Node();//Creation of node object and allocation of memory using new operator 
    node.next=null;//Since there is single node it point to null as there are no nodes after it
    node.value=inputvalue;//input value field
    head=node;//Memory address of node allocated to next of head as it id first element.
    tail=node;//Memory address of node allocated to next of tail as it is also last element.

    return head;
  } 
}
  1. Main.java class
class Main {
  public static void main(String[] args) {
    SinglyLinkedList sLL=new SinglyLinkedList();//sLL object creation and memory allocation
    sLL.createsSinglyLinkedList(5);//Value inserted
    System.out.println(sLL.head.value);//Linked list printed
  }
}

这是我的问题:

1.System.out.println(sLL.head.value);给出正确的输出 5。但是,System.out.println(sLL.node.value) 显示 Symbol not found?是因为Node node=new Node(); 创建作为局部变量的节点对象,而 head 是可以由 sLL 对象访问的实例变量。

2.If 节点对象存储在堆内存或栈内存中确实是一个局部变量,因为在某处读取局部变量存储在堆栈内存中。

3.Will开始存放在栈内存还是堆内存?

4.Are 这些陈述正确:

head=node;//分配给head的下一个节点的内存地址,因为它是第一个元素。

tail=node;//分配给tail的下一个节点的内存地址,因为它也是最后一个元素。

5.Also,(sLL.head.value) 到底是做什么的?

SinglyLinkedLists 的概念是,你有一个节点,表示为 head,从那里你可以遍历下一个音符,直到你遇到一个空节点(在某些情况下有一个预定义的监护人相反)。

public class Node{
  public int value;
  public Node next;
}

public class SinglyLinkedList{
  public Node head;
  private Node guardian; // no one needs to know the guardian

  public SinglyLinkedList(){
    guardian = new Node();
    head = guardian;
  }

  public void add(int inputvalue){
    // Create and set new node
    Node newNode = new Node();
    newNode.value(inputvalue);

    // append it to the chain
    newNode.next = head.next;

    // the newly added node is now the new head
    head = newNode;
  }

  public boolean contains(int valueToCheck){
  Node iterator = head;  
  while(iterator != guardian){
    if (iterator.value == valueToCheck){
      return true;
    }
    iterator = iterator.next;
  }

    return false;
  }
}

在初始化中您没有设置任何值。

A​​ DoublyLinkedList 的工作方式类似,只是您可以从两个方向追加和搜索。在这些情况下,您必须小心不要在穿越时下冲。但是您可以对两个阈值使用相同的监护人。

  1. System.out.println(sLL.node.value) shows Symbol not found? Is is because Node node=new Node(); creates node object that is local variable while head is instance variable that can be accessed by sLL object

是的。对象可以访问实例变量,不能访问局部变量,这里node是实例变量。

  1. If it is really a local variable will the node object be stored on heap memory or stack memory

局部变量存储在堆栈内存中,堆栈对象根据程序执行弹出。

  1. Will start be stored in stack or heap memory?

只有head没有start,head的值会随着列表中元素的增加而不断变化。 head 本身会存储在堆内存中,因为它是实例变量。

  1. Also,what does (sLL.head.value) actually do?

它将获取当前分配为 head

的节点的值 (int)