Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to create an (ordered) linked list of (ordered) linked lists. The list-of-list links are carried by the first nodes of its member lists. I am trying to achieve this via the following code, but my program crashes when I try displaying the second list. First list displays perfectly.

Here's a schematic of the data structure I am trying to construct: Image of construct

Please give me a solution, I have tried everything from using lists pointing to next list and so on and also trying to create another node pointer with first node as first node of first list. I also tried insertion at end for list of linked lists first nodes.

Please fix the problem and help me asap.

Code:

C++
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

struct node{
   int number;
   struct node*next;
   struct node*lsnext;
};

typedef struct node Node;
Node* insertValue(Node * list, int value);
void display(Node*);
Node* insertArr(Node * list, int value);
Node* addNodeBottom(int val, Node *start);

int main()
{

Node *globalList = NULL, *lists,*start,*save;
int nbrOfLists, listNo, nbrOfVal, valNo, val;

start=NULL;

printf("\n Enter the number of lists:");
scanf("%d", &nbrOfLists);

if(nbrOfLists < 0)
    return -1;

for(listNo = 0; listNo < nbrOfLists; listNo++)
{
    printf("\n\n Enter the number of inputs to the list %d: \n ",listNo+1);
    scanf("%d", &nbrOfVal);
    lists = NULL;

    for(valNo = 0; valNo < nbrOfVal; valNo++)
    {
        printf("Enter node value %d:", valNo+1);
        scanf("%d", &val);
        // Here we insert the value in both lists
        lists= insertValue(lists, val);
        globalList = insertValue(globalList, val);
    }

    start=addNodeBottom(val,lists);
    if(listNo==0){
        save=start;
    }
    printf("\n  The list %d is: ",listNo+1);
    display(lists);

}
   printf("\n\n The final list is: ");
   display(globalList);
   printf("The first list is");
   display(save);
   printf("The second list is");
   display(save->lsnext);  //crashes here
   return 0;
}

Node* insertValue(Node * list, int value)  //to insert node at the end
{
   Node *newNode, *m;
   newNode = malloc(sizeof(Node));
   newNode->number=value;

  if(list == NULL)
  {
     newNode->next=NULL;
     return newNode;
   }

  if(value < list->number)
  {
      newNode->next = list;
      return newNode;
  }

 m = list;
 while(m->next)
 {
     if(value < m->next->number)
        break;
     m = m->next;
 }
 newNode->next = m->next;
 m->next = newNode;
 return list;
 }

Node* addNodeBottom(int val, Node *start)
{
    Node*rear;
    Node* node1=(Node*) malloc(sizeof(Node));

    node1->number=val;
    node1->lsnext=NULL;

    if(start==NULL){
        start=node1;
    }
    else{
        start->lsnext=node1;
        start=node1;
    }
  return start;
 }

 void display(Node*nodex){

    while(nodex)
    {
        printf("%d ->",nodex->number);
        nodex=nodex->next;
    }
  }
Posted
Updated 8-Mar-15 11:21am
v3
Comments
Sergey Alexandrovich Kryukov 8-Mar-15 16:39pm    
Did you notice that your code had
#include
#include
#include
?

I tried to fix the formatting for you, please click "Improve question" and see how to do it in future. Structures as linked lists created from scratch is one of the fundamental and very important programming skills, you have to master it well. Use the debugger in any doubt.

—SA
Member 11508098 8-Mar-15 16:43pm    
Thanks I am new here. Thanks a ton for your help. I really hope for correct answer here.
Sergey Alexandrovich Kryukov 8-Mar-15 16:46pm    
You are welcome. Now, this is really your turn. You need to provide steps to reproduce your problem. Show what did you expect from some test set, what you got and why do you feel it's wrong. Before posting all that information, also execute those test samples under the debugger, try to notice until what point it code it works correctly, at what point some problems start.
—SA

1 solution

The addNodeBottom function is broken: inside it, in the statements
C
rear->lsnext=node1;
rear=node1;

you are using the uninitialized value rear.
 
Share this answer
 
Comments
Member 11508098 8-Mar-15 17:22pm    
Dude I have updated the code and changed rear with start. Hope it helps. However, still doesn't work
Sergey Alexandrovich Kryukov 9-Mar-15 0:42am    
Nothing will help you if you want to count on others to find your bugs. One is found for you. Find next one by yourself.
—SA
Sergey Alexandrovich Kryukov 9-Mar-15 0:41am    
5ed.
—SA
CPallini 9-Mar-15 12:44pm    
Thank you, Sergey.
Member 11508098 9-Mar-15 14:22pm    
So that bug was something I fixed already but was not reflected in the above code. If I could fix my bugs why would I post here as a question? What is the use of this forum.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900