简体   繁体   English

没有头或尾的Java循环链表?

[英]Java circular linked list without a head or tail?

My book only mentions circular linked lists on one page and says that you can create them by making the head and tail of single or double linked lists linked to each other. 我的书只在一个页面上提到了循环链表,并说你可以通过将单链表或双链表的头部和尾部相互链接来创建它们。 But then the programming exercise says: 但随后编程练习说:

"A circular-linked list has no need of a head or tail. Instead, you need only a reference to a current node, which is the nextNode returned by the Iterator. Implement such a class. For a nonempty list, the Iterator.hasNext method will always return true." “循环链接列表不需要头部或尾部。相反,您只需要引用当前节点,这是Iterator返回的nextNode。实现这样的类。对于非空列表,Iterator.hasNext方法将永远返回true。“

I'm not really sure how I should approach this. 我不确定我应该如何处理这个问题。

The exercise is worded in a way not to limit you in your implementation decision: rather than prescribing a particular solution, it lets you implement the list in a way that you find most convenient. 练习的措辞不会限制您的实施决策:它不是规定特定的解决方案,而是让您以最方便的方式实现列表。

You do need to have a pointer into the list, but since the list is circular, it does not need to point anywhere in particular. 你需要有一个指向列表的指针,但由于列表是循环的,因此不需要特别指向任何地方。 Since it does not point to a head or a tail, you can call it next , and keep it pointing to any element that you find convenient: 因为它并不指向一个头部或尾部,你可以把它叫做next ,并保持其指向你觉得方便的任何元素:

  • After insertion, next could point to the element that you have just inserted 插入后, next可以指向刚刚插入的元素
  • After deletion, next could point to the element after or before the deleted one 删除后, next可以指向删除之后或之前的元素
  • After a search, next could remain unchanged 搜索后, next可以保持不变

In order to convert your single or double linked list to circular, ul link the head and tail.. now the list structure is circular.. so it is not necessary to have a head / tail. 为了将你的单链表或双链表转换为圆形,ul链接头部和尾部..现在列表结构是圆形的......所以没有必要有头/尾。 Bcoz all nodes are interlinked and so no pointer has next node as null. Bcoz所有节点都是相互链接的,因此没有指针将下一个节点作为null。

And circular list is of 2 types. 循环列表有两种类型。 Single circular list - has hasNext() method nly Double circular list.- has hasNext() and hasPrev() 单循环列表 - 具有hasNext()方法nly双循环列表.-具有hasNext()和hasPrev()

The above mentioned methods are the ways of traversing in the circular linked list. 上述方法是遍历循环链表的方法。

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

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