1

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.

6
  • Is the length the number of items until you get back where you started? Commented Apr 26, 2019 at 8:55
  • 1
    Show us the code. Then show us what you've tried and what error you get. It is impossible to help you otherwise Commented Apr 26, 2019 at 8:55
  • @doctorlove yes Commented Apr 26, 2019 at 8:55
  • Those requirements sound arbitrary and a bit strange. Can you post the requirements verbatim instead of paraphrasing? Commented 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. Commented Apr 26, 2019 at 9:13

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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?
You don't even need to check the key. You can simply determine the identity of each element by its address in memory.
i cant use an Auxiliary pointer i must use the binary key filed to find the length
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.
1

You can browse your list in a way, counting how many elements you have until you come back to the starting element !

3 Comments

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
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
No i must use the boolean field

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.