更新时间:2023-11-10 09:32:52
快速浏览一下,您的列表似乎不仅是双向链接的,而且在末端也是相连的(所以它更像是一个 Ring 而不是 like一个列表).换句话说,如果我要遍历您的列表(包含元素 A、B、C、D
),它不会是:
Just from a quick skim, it seems that your list is not only doubly linked, but also is connected at the ends (so it's more like a Ring than like a list). In other words, if I were to iterate over your list (containing elements A, B, C, D
), it wouldn't be:
A -> B -> C -> D -> stop
应该是
A -> B -> C -> D -> A -> B -> C -> D -> A -> B ..... etc.
我怀疑这可能是您出现无限循环的原因.
I suspect that could be why you are having an infinite loop.
我会在您的 DoublyLinkedList
类中创建对列表最后一个元素的引用(例如:in.last
),使用它来获取最后一个元素,然后将第一个和最后一个元素链接到 null
或某种 NullListElement extends ListElement
I would create a reference to the last element of your list in your DoublyLinkedList
class (example: in.last
), use that for getting the last element, and have the first and last elements link to either null
or some sort of NullListElement extends ListElement
如果您必须将其保留为环,我仍然会添加对您列表的最后一个元素的引用,以便您可以这样说:
If you must keep it as a ring, I will still add a reference to the last element of your list, so that you can say something like:
if(walker == in.last) break; // stop