Build and print a linked list where every node represents a linked list in C

As title say I'm trying to build a list where every node point to another list, something like this:

   5 -> 10 -> 19 -> 28  <--- principal list
   |    |     |     |
   V    V     V     V
   7    20    22    35
   |          |     |
   V          V     V
   8          50    40
   |                |
   V                V
   30               45

Every node is the head of another list(as you can see in the picture). The problem is that when I try to print the list every node has only the latest element added to his own list, something like this:

   5 -> 10 -> 19 -> 28  <--- principal list
   |    |     |     |
   V    V     V     V
   7    20    22    35  <--just the last inserted node is added to 
                           every node of the list

My suspect is that the function that add "pages" to nodes somewhat overwrite every number that I write and because of that only the last one is showed. At support of this I've tried to add "manually" 2 "pages" to every node(the line of the code wrapped in comments) and in this case everything work correctly, so this should prove that print function work fine.

Here's the code

#include<stdio.h>
#include<stdlib.h>

struct page
{
    int val;
    struct page *prox;
};

struct node
{
    int date;
    struct node *next;
    struct page *page;
};

typedef struct page* page;
typedef struct node* node;

node newNode()
{
    node Node = malloc(sizeof(node));
    return Node;
}

page newPage()
{
    page page = malloc(sizeof(page));
    return page;
}

void printList(node Node)
{
    node T = Node;
    while(T != NULL)
    {
        printf("%d-->", T->date);
        while(T->page != NULL)
        {
            printf("%d  ", T->page->val);
            T->page = T->page->prox;
        }
        printf("\n ยง\n");
        T = T->next;
    }

    printf("NULL\n");
}

void inputPage(node Nodee)
{
    node Node = Nodee;
    Node->page = newPage();
    scanf("%d", &Node->page->val);
    for(int x=0; x<1; x++)
    {
        Node->page = Node->page->prox = newPage();
        scanf("%d", &Node->page->val);
    }
}

int main()
{
    node Node = newNode();
    scanf("%d", &Node->date);
     inputPage(Node);
        /*Node->page = newPage();
        Node->page->val = 1;
        Node->page->prox = newPage();
        Node->page->prox->val = 2;*/

    node node = Node;
    for(int y=0; y<1; y++)
    {
        node = node->next = newNode();
        scanf("%d", &node->date);
        inputPage(node);
        /*node->page = newPage();
        node->page->val = 1;
        node->page->prox = newPage();
        node->page->prox->val = 2;*/
    }

    printf("\n");
    printList(Node);
}

2 answers

  • answered 2018-02-13 01:37 EmptyArsenal

    The crux of the problem is that when you're trying to iterate through pages, you're setting node->page to the next page, which keeps overwriting node->page rather than diving deeper.

    while(T->page != NULL)
    {
        // You keep overwriting node->page until it gets null
        T->page = T->page->prox;
    }
    T = T->next;
    

    Iterating through should be more like this:

    page p = T->page;
    while (page != NULL) {
        // print page->val
        page = page->prox;
    }
    // Now, go to next node
    

    Your inputPage has a similar problem, but it's kind of a mess:

    node Node = Nodee;
    Node->page = newPage();
    scanf("%d", &Node->page->val);
    // Need to track most recent page here.
    for(int x=0; x<1; x++)
    {
        // We don't care about Node at this point. Just track the last page
        // since the pages are already attached to the node.
        Node->page = Node->page->prox = newPage();
        scanf("%d", &Node->page->val);
    }
    

  • answered 2018-02-13 01:37 Abhijit Pritam

    EmptyArsenal already highlighted some of your mistakes. I am adding some more advise here. Always initialized properly like below especially pointer:-

    node newNode()
    {
      node newNode = malloc(sizeof(node));
      newNode->next = NULL;
      newNode->page = NULL;
      return newNode;
    }
    
    page newPage()
    {
      page newpage = malloc(sizeof(page));//avoid using duplicate variable name
      newpage->prox = NULL;//code should not be confusing
      return newpage;
    }
    

    now modify your inputPage function like below:-

    void inputPage(node Node)//don't write code which may make you more confuse
    {
       if(Node->page == NULL)//check whether list has been created already or not
       {
         Node->page = newPage();//if node list is empty
         scanf("%d", &Node->page->val);
         return;
       }
    
        //node temp = Node;// we don't need this line of code 
       //we need a page pointer here
       page p = Node->page;//we are pointing to the first page of a Node
    
       //for(int x=0; x<1; x++)//not a proper loop at all we don't need it
       //here you need to traverse through the page list
       while(p->prox != NULL) //traverse till you reach last page node
       {
           p = p->prox;
       }
           p->page= newPage();//p is now pointing to the last node of page sub list
           scanf("%d", &Node->page->val);
    }
    

    Now modify your main function accordingly and check. If you need any more help please let me know. GOOD LUCK!