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

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

问题描述

我正在用 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 ->空

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: 所以如果 head12head.next34>,那么不就意味着下面这个函数跳过第一个节点看它是否为空?

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 方法跳过了第一个元素,并且在某种意义上确实如此.这是因为实现有一个虚拟"元素作为头部.看构造函数中头部变量的初始化:

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天全站免登陆