Thread: printing another field of nodes in a linked list without going over the list again

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    180

    printing another field of nodes in a linked list without going over the list again

    Say i want to go over a linked list and just count the nodes that satisfy a particular condition. Later i would print value of some field in those nodes. I could go over the list again but the bigger the list the more time it would take.
    I think i read before that one option is to store the pointers of the nodes of interest in an array of pointers. Is that a good solution? Are there better solutions?
    I imagine there will always be comprimise between time and space. Would like to know what is the best solution for this problem.
    Last edited by telmo_d; 10-19-2015 at 04:13 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,922
    Quote Originally Posted by telmo_d
    I think i read before that one option is to store the pointers of the nodes of interest in an array of pointers. Is that a good solution?
    Yes.

    Quote Originally Posted by telmo_d
    Are there better solutions?
    Perhaps you could print them even as you count them (i.e., if you do not need to display the count before printing the first matching node). The bad part is that this tightly couples the counting and the printing.
    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 2015
    Posts
    180
    I do need to count first, later do other operation on the nodes.

    I tried creating array of pointers but it giving seg fault.

    Code:
    Node** array = calloc(100, sizeof(Node));
    array[0] = head;
    It seg faults right there, what's the problem?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,922
    The problem is that you used sizeof(Node) instead of sizeof(Node*). To avoid such problems, get into the habit of making use of the array name in the sizeof expression, e.g.,
    Code:
    Node** array = calloc(100, sizeof(array[0]));
    You should also check that calloc did not return a null pointer before assigning to array[0].
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing info from nodes in a linked list
    By Kati Baker in forum C Programming
    Replies: 3
    Last Post: 10-16-2013, 09:53 AM
  2. Linked list and nodes
    By Lina_inverse in forum C Programming
    Replies: 2
    Last Post: 10-23-2012, 01:59 PM
  3. Linked List of nodes with operators
    By antonsavelyev in forum C Programming
    Replies: 3
    Last Post: 04-14-2011, 05:30 PM
  4. deleting from a linked list if there are just 2 nodes
    By Nooby in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2010, 11:55 PM
  5. Linked List and Nodes
    By paperbox005 in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2004, 09:12 AM