Thread: Lists.

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    58

    Post Lists.

    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?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,930
    Yes, of course: just traverse the circular linked list counting node by node until you have, um, gone full circle
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    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).

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,930
    Quote Originally Posted by TheGreekMan2000
    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.

    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    Thanks very much for your time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays, Linked Lists and Unordered Lists
    By bob432 in forum C++ Programming
    Replies: 4
    Last Post: 02-03-2020, 04:01 AM
  2. Replies: 3
    Last Post: 02-23-2013, 06:11 AM
  3. Double Linked Dynamic Lists Vs Unrolled Linked Lists
    By lantzvillian in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 01:07 PM
  4. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 02:49 AM
  5. question on linked lists(stack with linked lists)
    By dionys in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 11:08 AM

Tags for this Thread