简体   繁体   English

从头开始实现 Java.Util 链表

[英]Java.Util Linked List Implementation from Scratch

I would like to implement a singly linkedlist with functions that the java.util library does not contain such as: returning the middle node in a linked list and inserting a new head at the linkedlist.我想用 java.util 库不包含的函数实现一个单链表,例如:返回链表中的中间节点并在链表中插入一个新头。 My question is: What type of linked list is the java.util linked list (eg. singly, doubly)?我的问题是:java.util 链表是什么类型的链表(例如,单独的、双重的)? Secondly, I would be interested in knowing if you recommend me to implement linked lists from scratch.其次,我很想知道您是否建议我从头开始实现链表。 I care about being able to maintain my own code without third party libraries (because of the dependency) and being able to customise it.我关心能够在没有第三方库的情况下维护我自己的代码(因为依赖关系)并且能够自定义它。 I also want to implement merge sort and I feel that having a middle node method is important.我也想实现归并排序,我觉得有一个中间节点方法很重要。 Thanks!!谢谢!!

The Java implementation of LinkedList is a doubly-linked list. LinkedList的 Java 实现是一个双向链表。 Also, it already has the ability to add an element at the head/front (if I understood correctly).此外,它已经能够在头部/前端添加一个元素(如果我理解正确的话)。

Linked lists are relatively simple, so you can implement them from scratch if you like (there are plenty of tutorials if you do a simple search as well).链表相对简单,因此您可以根据需要从头开始实现它们(如果您进行简单的搜索,也会有很多教程)。 However, if a third-party open source library meets your needs, I would highly recommend using it.但是,如果第三方开源库满足您的需求,我强烈建议您使用它。 It is likely to be far better maintained than anything you can manage.它可能比您可以管理的任何东西都得到更好的维护。 If you think it is falling behind or lacking, you can offer contributions.如果你认为它落后或缺乏,你可以提供贡献。

For merge sort, not sure why you need it.对于合并排序,不确定为什么需要它。 Java collections already implements merge sort , and uses the most efficient and up to date algorithms available. Java 集合已经实现了归并排序,并使用了最有效和最新的可用算法。

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

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