简体   繁体   English

链表中的节点到底是什么?

[英]What exactly are the nodes in linkedlists?

https://www.geeksforgeeks.org/linked-list-set-1-introduction/ https://www.geeksforgeeks.org/linked-list-set-1-introduction/

https://beginnersbook.com/2013/12/linkedlist-in-java-with-example/ https://beginnersbook.com/2013/12/linkedlist-in-java-with-example/

is there a diffence? 有区别吗? Like why does the geek one have likes nodes 就像为什么怪胎有喜欢节点

    llist.head = new Node(1); 
    Node second = new Node(2); 
    Node third = new Node(3); 

But the other link did 但是另一个链接确实

  list.add("Steve");
  list.add("Carl");
  list.add("Raj");
  list.add("Negan");
  list.add("Rick");

like I feel like either im just overthinking it, or the the sites arent clearly explaining things. 就像我觉得自己只是想得太多,还是这些站点没有清楚地解释事情。 the linkedlists seem to be functionally the same as arraylists. 链表似乎在功能上与数组列表相同。

I basically did my arraylist assignment, and really only changed 我基本上做了我的arraylist分配,实际上只是改变了

LinkedList list from ArrayList list ArrayList列表中的LinkedList列表

    for(int i = 0; i < listSize; i++)
    {
        System.out.print("Enter a number to go in the list: ");
        int num = scan.nextInt();
        list.add(num);
    }

and everything functions fine, so what are all these nodes, and stuff that geeks site did? 一切正常,那么这些节点以及极客站点所做的工作又是什么呢?

An ArrayList is a list which is backed by an array with additional features to grow dynamically, etc. These lists are also random access which means you can specify the exact location via an index. ArrayList是一个由数组支持的列表,该数组具有可动态增长的其他功能,等等。这些列表也是random access ,这意味着您可以通过索引指定确切的位置。

A linkedList has what are call nodes. linkedList具有什么是调用节点。 In Java, nodes are simple classes that have at least two fields. 在Java中,节点是具有至少两个字段的简单classes One for the next node and one for the data. 一个用于下一个节点,一个用于数据。 In other languages they can be structures . 在其他语言中,它们可以是structures They are not random access because to find a certain element you would have to start at the head (top node) and count as the list is traversed. 它们不是随机访问的,因为要找到某个元素,您将必须从头(顶部节点)开始并在遍历列表时计数。 The nodes themselves are of an internal nature and are of no concern to the user. 节点本身具有内部性质,并且与用户无关。

Preference of one over the other should be based on how it is to be used. 一个优先于另一个应基于如何使用它。 How often it needs to be updated and speed of access are considerations, among others. 除其他事项外,还需要考虑更新频率和访问速度。

As previously answered the two examples you gave are just different implementations of the same concept. 如前所述,您给出的两个示例只是同一概念的不同实现。 The main point you should take away from this is that a linked list is composed of various nodes which point to each other and the list has a head. 您应该从中获得的要点是,链表由指向彼此的各种节点组成,并且链表具有头部。 Inserting an element to the list consists of (implicitly or explicitly) creating a node and adding it to the end while deleting an element requires first searching for the element one node at a time and removing it from the list once found. 将元素插入列表包括(隐式或显式)创建节点并将其添加到末尾,同时删除元素需要首先一次搜索一个节点并将其从列表中删除。

Now what is all the business about nodes? 现在,关于节点的所有业务是什么?

Well, nodes can be thought of as containers for data that also point to other nodes. 好吧,可以将节点视为也指向其他节点的数据的容器。 The simplest LinkedList is a singly-linked LinkedList, meaning that each nodes "points" to the next node in the list. 最简单的LinkedList是单链接的LinkedList,这意味着每个节点都“指向”列表中的下一个节点。

The implication of this is that you technically never have to implement a LinkedList and can instead just make a node class and create a node that is treated as the head of the list. 这样做的含义是,从技术上讲,您不必实现LinkedList,而只需制作一个节点类并创建一个被视为列表头的节点即可。 Then, since you have the head you can then access any node in that list because each node points to each other, you just have to search one by one. 然后,由于您拥有头,因此您可以访问该列表中的任何节点,因为每个节点都指向彼此,因此您只需要一个一个地搜索即可。 Same thing with adding an element to the list; 将元素添加到列表也是一样; start with the head and check if it points to another node, if it doesn't create a new node and set it as the "next" node after head. 从头开始,检查它是否指向另一个节点,如果它没有创建新节点,则将其设置为头之后的“下一个”节点。 On the other hand if it didn't have a next node that head points to check if that next node points to another node and just repeat until you find the last node in the list which doesn't point to anything. 另一方面,如果它没有下一个节点,则其头部指向检查该下一个节点是否指向另一个节点,然后重复进行直到找到列表中未指向任何内容的最后一个节点。 Deleting a node would require a similar process of iterating through each node until you find the one you want to delete, then remove it. 删除节点将需要类似的过程来遍历每个节点,直到找到要删除的节点,然后再将其删除。 This implementation is what the first example is showing. 第一个示例显示了此实现。

On the other hand in the second example they've actually implemented the LinkedList which fundamentally operates the same way as I just described only that the user no longer has to write a loop to find the last node in a list, create a node, and finally set it as the next node. 另一方面,在第二个示例中,他们实际上已经实现了LinkedList,该操作基本上与我刚刚描述的相同,只是用户不再需要编写循环来查找列表中的最后一个节点,创建节点以及最后将其设置为下一个节点。 Instead, to add a node they just call add(node) and the LinkedList class will deal with all the in between steps. 相反,要添加节点,他们只需调用add(node),LinkedList类将处理步骤之间的所有操作。

Implementation 实作

Whichever implementation you choose will produce the same result but I do want to warn you about using an ArrayList. 您选择的任何实现都会产生相同的结果,但是我确实要警告您有关使用ArrayList的信息。 In a way you've kinda gotten lucky since the ArrayList add() method always adds to the end of the list similarly to how a LinkedList works. 在某种程度上,您很幸运,因为ArrayList的add()方法总是类似于LinkedList的工作方式添加到列表的末尾。 You can get away with all LinkedList operations (that I can think of) using an ArrayList but fundamentally these two operate very differently in terms of amount of memory they use and how well they perform when adding, finding or deleting an element. 您可以使用ArrayList摆脱所有LinkedList操作(我可以想到),但是从根本上来说,这两个操作在使用的内存量以及添加,查找或删除元素时的性能方面有很大不同。 Lasly, you just might not be able to take away the key points of a LinkedList without implementing one yourself. 有时,如果您自己没有实现,您可能无法消除LinkedList的关键点。

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

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