I need some explanation with the logic in the following program:
Create a data structure such that it contains of three singly linked lists that are connected to one central node.
i-th list can only have elements such that list_element % 3 = i.
First element of every list represents it's index (i).
Example of this data structure:

             root
           /   |   \
          0    1    2
          |    |    |
          3    4    5
          |    |    |
         12    7    8

Write a program that adds one element in specified list (it can't contain duplicate elements), sorts specified list,
removes one element from specified list and prints specified list.
I have defined this data structure as following:

typedef struct list_node
{
    int info;
    struct list_node *next;
}LIST_NODE;

typedef struct root_node
{
    LIST_NODE plist;
    struct list_node *first;
    struct list_node *middle;
    struct list_node *last;
}ROOT_NODE;

Then, defined functions that manipulate with only one linked list:

void addToList(LIST_NODE *plist,int data)
{
    LIST_NODE *lnode=(LIST_NODE *)malloc(sizeof(LIST_NODE));
    lnode->info=data;
    lnode->next=NULL;
    if(plist == NULL)
        plist=lnode;
    else
    {
       LIST_NODE *temp=plist;
       while(temp->next)
           temp=temp->next;
       temp->next=lnode;
    }
}

void sortList(LIST_NODE *plist)
{
   for(; plist && plist->next; plist=plist->next)
   {
      LIST_NODE *min=plist,*p;
      for(p=plist->next; p; p=p->next)
         if(min->info > p->info)
            min=p;
      if(min != plist)
      {
         int temp=plist->info;
         plist->info=min->info;
         min->info=temp;
      }
   }
}

int deleteFromList(LIST_NODE *plist)
{
   LIST_NODE *q=plist->next;
   if(q)
   {
      plist->next=q->next;
      plist->info=q->info;
      free(q);
      return 1;
   }
   free(plist);
   return 0;
}

void printList(LIST_NODE *plist)
{
    while(plist)
    {
       printf("%d",plist->info);
       plist=plist->next;
    }
}

LIST_NODE* searchList(LIST_NODE *plist,int key)//for avoiding duplicate elements
{
   while(plist && plist->info != key)
         plist=plist->next;
   return plist;
}

For adding an element to specified list, sorting the specified list and printing the specified list:

void addToSpecifiedList(ROOT_NODE *root,int data)
{
   if(root->first->info == '0')
     addToList(root->first,data);
   if(root->middle->info == '1')
     addToList(root->middle,data);
   if(root->last->info == '2')
     addToList(root->last,data);
}

void sortSpecifiedList(ROOT_NODE *root)
{
      if(root->first->info == '0')
        sortList(root->first);
      if(root->middle->info == '1')
        sortList(root->middle);
      if(root->last->info == '2')
        sortList(root->last);
}

void deleteFromSpecifiedList(ROOT_NODE *root)
{
    if(root->first->info == '0')
        deleteFromList(root->first);
    if(root->middle->info == '1')
        deleteFromList(root->middle);
    if(root->last->info == '2')
        deleteFromList(root->last);
}

void printSpecifiedList(ROOT_NODE *root)
{
    if(root->first->info == '0')
        printList(root->first);
    if(root->middle->info == '1')
        printList(root->middle);
    if(root->last->info == '2')
        printList(root->last);
}

void sortSpecifiedList(ROOT_NODE *root)
{
      if(root->first->info == '0')
        sortList(root->first);
      if(root->middle->info == '1')
        sortList(root->middle);
      if(root->last->info == '2')
        sortList(root->last);
}

The logic of these last five functions is not correct.
How to access each list separately by index (first element of a list) and do the defined operations?
Also, how to restrict that the elements satisfies the condition list_element % 3 = i?

A helper function to choose the list will help cut down on duplicated code:

LIST_NODE* ChooseList(ROOT_NODE * root, int data)
{
    switch (data % 3)
    {
    case 0:
        return root->first;
    case 1:
        return root->middle;
    case 2:
        return root->last;
    default:
        printf("Invalid index");
        break;
    }
}

Now to each function you pass either the data or the index of the list, and assign a temporary variable to the return value of ChooseList.

    LIST_NODE tempRoot = ChooseList(ROOT_NODE* root,int data);

Now you can pass this to the appropriate function that you've already made.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.