链表的“头”是什么? [英] What is the 'head' of a linked list?

查看:293
本文介绍了链表的“头”是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java链接列表,因此我正在尝试掌握单个链表的概念。

I'm working in linked lists in Java, so I'm trying to grasp the concept of a single linked list.

head - > 12 - > 34 - > 56 - > null

head.next 将为12(也与node1相同)。然而,什么是头呢?

head.next would be 12 (also the same as node1). However, what is head then?

更新:参考和指针有什么区别?

Update: What is the difference between a reference and a pointer?

Update2:因此,如果 head 12 head.next 34 ,那么这并不意味着此后续函数会跳过第一个节点以查看它是否为空?

Update2: So if head is 12 and head.next is 34, then doesn't mean this following function skips the first node to see if it's null?

public void add(Object data, int index)
    // post: inserts the specified element at the specified position in this list.
    {
        Node temp = new Node(data);
        Node current = head;
        // crawl to the requested index or the last element in the list,
        // whichever comes first
        for(int i = 1; i < index && current.getNext() != null; i++)
        {
            current = current.getNext();
        }
        // set the new node's next-node reference to this node's next-node reference
        temp.setNext(current.getNext());
        // now set this node's next-node reference to the new node
        current.setNext(temp);
        listCount++;// increment the number of elements variable
    }

来源: http://www.mycstutorials.com/articles/data_structures/linkedlists

推荐答案

列表的头部是指列表的第一个节点。它会为存储该节点的引用的变量建立一个好名称,如果列表为空,我希望它包含空引用

The head of the list refers to the first node of the list. It would make a good name for the variable storing the reference of that node, and I would expect it to contain the null-reference if the list was empty

someLinkedList.head
         |
         |
         v
        ______        ______        ______            
       |    |n|      |    |n|      |    |n|           
       |    |e|      |    |e|      |    |e|           
       | 12 |x| -->  | 34 |x| -->  | 56 |x| --> null
       |    |t|      |    |t|      |    |t|           
       |____|_|      |____|_|      |____|_|           

根据上下文,尾部可以引用不同的东西。我以前用的术语说尾巴对应 34 - > 56 - >在此示例中为null ,即头部后面的列表。

Depending on context, the tail can refer to different things. The terminology I'm used to says that the tail corresponds to 34 -> 56 -> null in this example, that is, the list that follows the head.

在其他上下文中,它可能是对最后一个节点的引用。在这种解释中,尾部将引用示例中的 56 节点。

In other contexts, it may be a reference to the last node. In such interpretation, the tail would refer to the 56 node in your example.

关于你的第一次编辑,这恰好是完全不同的问题

Regarding your first edit, which happens to be a completely different question:

指针是一个对应于内存的值地址。引用是指引用某个对象(或null)的值。你不能对Java引用做指针算术,但是我会说它们非常相似。

A pointer is a value corresponding to a memory address. A reference is value referring to some object (or null). You can't do pointer arithmetic on Java references, but otherwise I'd say they are fairly similar.

可能让你困惑的是,Java中的变量可以永远不会包含对象。对象总是存在于堆上,变量包含原始数据类型或对堆上对象的引用。

What may confuse you, is that variables in Java can never contain objects. Objects always live on the heap, and variables contain primitive data types, or references to objects on the heap.

关于你的第二次编辑:

在您提供的示例中,看起来add方法会跳过第一个元素,从某种意义上说,它会跳过第一个元素。这是因为实现具有虚拟元素作为头部。查看构造函数中head-variable的初始化:

In the example you provided, it looks like the add method skips the first element, and in a sense it does. This is because the implementation has a "dummy"-element as the head. Look at the initialization of the head-variable in the constructor:

head = new Node(null);

我无法理解为什么他们决定这样做。对我来说,它看起来很愚蠢。

I can't understand why they've decided to do that. To me it looks plain stupid.

这篇关于链表的“头”是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆