Thread: Printing a list which has elements other lists (same struct) recursively.

  1. #1
    Registered User
    Join Date
    Jun 2018

    Printing a list which has elements other lists (same struct) recursively.

    This is how i print recursively a list

    if (mylist_ptr == NULL) {
            return ;
        printf("%s: ", mylist_ptr->value);
        return ;
    Now i want to print a list which has elements other lists,same way(recursively).
    This is how my list looks like ,but ofc i want it for any number of lists.

    Sorry for the quality..
    Printing a list which has elements other lists (same struct) recursively.-untitled-jpg
    EDIT:Each sublist ends with a node with a NULL in its next field
    Also the stucts for the sublists and the listoflsits :

    struct node {
        char value[20] ;
        struct node* next ;    
    } ;
    typedef struct node Node;
    typedef Node * List;
    struct listoflists{
        List list;
        struct listoflists*next;
    } ; 
    typedef struct listoflists Listoflists;
    Last edited by lltocl; 06-18-2018 at 09:36 AM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Long Beach, CA
    The way you have it defined, you can only have 2 levels of nesting. This means there's no need to do this recursively. You could simply do this with nested loops (while loops or for loops).

    The listoflists is really just a singly linked list whose data element is a List. Then the List is just a singly linked list whose data element is a char array. There's no way to make that List have a sublist, only a next element. Hence 2 levels of lists max.

    However, I suspect you actually want a truly recursive list of lists. In that case you need the node element to contain both a "sublist" element and a "next" element:
    struct node {
      char value[20];
      struct node *sublist;
      struct node *next;
    One of the biggest lessons in programming is that programming is not about writing code, it's about solving problems. If you can't solve a problem, you can't program a computer to solve it. So you must understand the problem and solution first, before you code.

    First, get pencil and paper, and draw out some fairly simple nested lists. Keep them short, and don't nest more than ~3 levels. Make sure you include different scenarios like lists with no sublists, as well as lists and sublists with only one element, etc. Figure out how you would like such lists to be printed. Working through the process of what you want to happen with paper and pencil (or whiteboard/whatever) is a huge part of successfully writing a program.

    Recall, recursive functions typically have two pieces: the base case (when does it stop) and the "keep going" case. Given the new struct, what do you think is a likely/good choice for a base case for the sublists? What should you do when you get to a last sublist (i.e. a node that has no sublist)? How would you "keep going" if there are more nested lists? Hint: there is a very strong parallel between recursively printing a single list, and recursively printing nested lists.

    Start by writing out some steps in plain English (or your native language), then slowly turn those into code.

    You could also start looking at N-ary trees, since that is basically what you have here.

    One more thing:
    Don't typedef pointers unless you're implementing an opaque data structure. Otherwise it just hides what the true type of the object is and makes it more likely to be misused, resulting in resource leakage or a variety of pointer bugs leading to memory corruption, program crashes, and other undefined behavior.

    You can, however, typedef the struct portion of it:
    #define VALUE_LEN 20
    typedef struct node Node;
    struct node {
      char value[VALUE_LEN];
      Node *sublist;
      Node *next;
    Oh, also note that VALUE_LEN. Avoid magic numbers in your code, instead using constants with accurate, descriptive names.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-16-2018, 08:30 AM
  2. printing std::tuple recursively
    By Dave11 in forum C++ Programming
    Replies: 5
    Last Post: 04-29-2015, 03:07 AM
  3. Linked list reverse recursively
    By gaurav_13191 in forum C Programming
    Replies: 4
    Last Post: 12-07-2011, 09:32 AM
  4. Recursively list files inside dir
    By b1nd3r in forum C Programming
    Replies: 2
    Last Post: 11-23-2010, 04:00 AM
  5. Add elements through array recursively
    By silicon in forum C++ Programming
    Replies: 7
    Last Post: 09-26-2003, 09:02 PM

Tags for this Thread