简体   繁体   English

查找圆形双链表长度的算法

[英]Algorithm to find Length of circular double linked list

I've built a circular two-way circular list and I want to find its length. 我建立了一个双向双向循环列表,我想找到它的长度。 I am allowed to use the function only in the Pointer to list. 我只允许在“指针”列表中使用该功能。 In the list I have a binary field. 在列表中,我有一个二进制字段。 An idea how can I do this? 一个想法,我该怎么做? I have to use the binary field, I can not use the auxiliary pointer and I have to have it in good complexity. 我必须使用二进制字段,我不能使用辅助指针,而且必须具有很好的复杂性。

Keep track of the element at which you start your traversal. 跟踪开始遍历的元素。 Start walking along the list and check for each element if it is the one where you started. 开始沿着列表走动,并检查每个元素是否是您开始的元素。 If it is not, you increment your count by one and continue on to the next element. 如果不是,则将计数增加一个,然后继续进行下一个元素。 If it is, you have visited every element and the current value of the count is your desired result. 如果是,那么您已经访问了每个元素,并且当前计数值是您想要的结果。

您可以通过某种方式浏览列表,计算拥有多少元素,直到返回起始元素!

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

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