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

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

This is how i print recursively a list

Code:
```if (mylist_ptr == NULL) {
return ;
}

printf("%s: ", mylist_ptr->value);
report(mylist_ptr->next);

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..

EDIT:Each sublist ends with a node with a NULL in its next field
Also the stucts for the sublists and the listoflsits :

Code:
```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;```

2. 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:
Code:
```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:
Code:
```#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.