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.
-
Is the length the number of items until you get back where you started?doctorlove– doctorlove2019-04-26 08:55:03 +00:00Commented Apr 26, 2019 at 8:55
-
1Show us the code. Then show us what you've tried and what error you get. It is impossible to help you otherwiseClonk– Clonk2019-04-26 08:55:09 +00:00Commented Apr 26, 2019 at 8:55
-
@doctorlove yesEden– Eden2019-04-26 08:55:42 +00:00Commented Apr 26, 2019 at 8:55
-
Those requirements sound arbitrary and a bit strange. Can you post the requirements verbatim instead of paraphrasing?molbdnilo– molbdnilo2019-04-26 09:11:45 +00:00Commented Apr 26, 2019 at 9: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– Eden2019-04-26 09:13:32 +00:00Commented Apr 26, 2019 at 9:13
|
Show 1 more comment
2 Answers
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.
4 Comments
Eden
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?
ComicSansMS
You don't even need to check the key. You can simply determine the identity of each element by its address in memory.
Eden
i cant use an Auxiliary pointer i must use the binary key filed to find the length
ComicSansMS
In 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.
You can browse your list in a way, counting how many elements you have until you come back to the starting element !
3 Comments
Eden
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
No i must use the boolean field