1

I want to create a main list and each of the main list element has another list.

here is what i did

    typedef struct smallList
    {   char data;
        struct smallList *next;  

     } small;

    typedef struct bigList
    {
        int count;
        char data;
        struct bigList *next;
        struct smallList *head;
     } big;

but how do I access the small list data from the big list and add stuff to the small list. any help much appreciated. thanks....

2
  • 7
    Hey dawg, I heard you liked linked lists, so we created a linked list inside a linked list so you can enjoy a linked list while you iterate though another linked list. Commented Feb 28, 2013 at 19:31
  • This sounds like the question from yesterday (stackoverflow.com/questions/15094665/…) where a poster needed to make a linked lists where each item held another linked list for a class assignment. That didn't get an answer posted at the time. Commented Feb 28, 2013 at 19:37

2 Answers 2

2

So, if we assume that this structure is already populated, we could do:

struct smallList *smallElem = NULL;
struct bigList *bigElem = NULL;

for (bigElem = your_big_list(); bigElem != NULL; bigElem = bigElem->next) {
    // Do something with bigElem.

    for (smallElem = bigElem->head; smallElem != NULL; smallElem = smallElem->next) {
        // Do something with the smallElem.
        // Note that we can still reference bigElem here as well.
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for the help. does "your_big_list()" returns the address of the first element(or any element) in big List.
@user1476263: I would likely use this with the first element of the list, but it will iterate starting at any given element through the end of the list.
1

If p points to a bigList:

  • bigList -> head is the small list that bigList points.
  • (bigList -> head).data is the character the smallList contains.
  • (bigList -> next -> head) is second smallList in bigList.
  • (bigList > head -> next) is the second element in bigList's first smallList.

After getting a pointer to the structure you want to modify, everything else is the same.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.