Can I count how many nodes I have created in a circular self-referencial list if I don't know in advance how many nodes to add?
Can I count how many nodes I have created in a circular self-referencial list if I don't know in advance how many nodes to add?
Yes, of course: just traverse the circular linked list counting node by node until you have, um, gone full circle
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Can I, also, in a circular linked self-referencial list when done the full circle, find and print in the terminal the nodes that have a part of a string? For example, I want to print the nodes that have any form of it (it, It,iT,IT).
Since you have to ask, obviously, no, you can't.Originally Posted by TheGreekMan2000
But I can
The thing is, when you are faced with a problem like this, you need to break it down into smaller problems that you can solve. Like, ask yourself: given a single node, can you find out if its data has a substring of the given form? Surely you can: you search the string data to see if it contains the substring. If you're not sure how to do this, then temporarily put aside your linked list and write an exploratory test program in which you construct strings of the given form (and not), and come up with code that does this searching for a substring in the given form. When you're confident that you have a solution, you then go back to your linked list and incorporate your solution into your actual program.
So, given a single node, now you can find out if its data has a substring of the given form. Hence, given a circular linked list, you can easily print all the relevant nodes: you just loop over the nodes and apply the logic for a single node, node by node.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thanks very much for your time.