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.
Asked
Active
Viewed 469 times
1
-
Is the length the number of items until you get back where you started? – doctorlove Apr 26 '19 at 08:55
-
1Show us the code. Then show us what you've tried and what error you get. It is impossible to help you otherwise – Clonk Apr 26 '19 at 08:55
-
@doctorlove yes – Eden Apr 26 '19 at 08:55
-
Those requirements sound arbitrary and a bit strange. Can you post the requirements verbatim instead of paraphrasing? – molbdnilo Apr 26 '19 at 09:11
-
An efficient program that receives as a pointer input must be written to the list and returns the length of the list (The number of nodes in it). You can not use another pointer, but you can change the value The binary at each node from 0 to 1 and vice versa. – Eden Apr 26 '19 at 09:13
-
I suspect this is a duplicate of https://stackoverflow.com/questions/6482886/cycle-detection-in-linked-list-with-the-hare-and-tortoise-approach - or at least can extend what's in it. That at least shows you the class algo for cycles in lists – doctorlove Apr 26 '19 at 09:20
2 Answers
3
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.

ComicSansMS
- 51,484
- 14
- 155
- 166
-
I have a key in the field, or 0 or 1 because it is Boolean, if I reset everything and then I go over everything and check 1 it will be a good complexity, is there a better method? – Eden Apr 26 '19 at 08:58
-
1You don't even need to check the key. You can simply determine the identity of each element by its address in memory. – ComicSansMS Apr 26 '19 at 09:01
-
i cant use an Auxiliary pointer i must use the binary key filed to find the length – Eden Apr 26 '19 at 09:05
-
1In such an arbitrarily constrained case, you have to establish identity through other means. Your suggestion from above seems to be a reasonable approach in that case. Note though that resetting everything is a not as easy as it sounds if you don't already know the length of the list. A better approach would be to establish an invariant that gives you knowledge of the value of each key before you start the traversal. – ComicSansMS Apr 26 '19 at 09:07
1
You can browse your list in a way, counting how many elements you have until you come back to the starting element !
-
but i dont know the first one it is a circle i can get something like that 1 0 1 0 1 0 1 the last 1 point to the first 1 – Eden Apr 26 '19 at 17:04
-
Can't you just check the pointer adress instead of a field of your list ? You get a pointer on the list, get its adress and then circles until you come back to this address – Apr 26 '19 at 18:20
-