Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: C Program #13

Sunday, March 10, 2013

C Program #13


/* Given a large linked list, L of integers such that L=(L0,L1,L2,..............,Ln). Write a program to the program into K list M1, M2, M3,.........., Mk such that
M1= L0,L0+k,L0+2k,..............
M1= L1,L1+k,L1+2k,..............
And so on */

#include<stdio.h>
#include<malloc.h>
struct list
                        {
                        int item;
                        struct list *next;
                        };
typedef struct list node;
int element(node *,int);
int main()
{
node *root[20],*p,*q;
int list_size=0,K,i,j;
root[0]=(node *)malloc(sizeof(node));
p=root[0];
printf("\n Enter the elements of the main list(enter -999 to exit):");
scanf("%d",&root[0]->item);
            while(1)
            {
             if(p->item==-999)
                        break;
             p->next=(node *)malloc(sizeof(node));
             p=p->next;
             scanf("%d",&p->item);
            }
p->next=NULL;
printf("\n Enter the elements of the list:");
p=root[0];
while(p->next!=NULL)
            {
            printf("%d ",p->item);
            p=p->next;
            list_size++;
            }
printf("\n Enter the number of lists to be formed:");
scanf("%d",&K);
for(i=1;i<=K;i++)
            {
            root[i]=(node *)malloc(sizeof(node));
            q=root[i];
            for(j=i;j<=list_size;j++)
                        {
                        q->item=element(root[0],j);
      q->next=(node *)malloc(sizeof(node));
      q=q->next;
                        //printf("%d ",q->item);
                        j=j+K-1;
                        }
            q->next=NULL;
            }
for(i=1;i<=K;i++)
            {
            printf("\n Elements of List M[%d]:\n",i);
                        while(root[i]->next!=NULL)
                                    {printf("%d ",root[i]->item);
                                                root[i]=root[i]->next;
                                    }
            printf("\n");
            }
return 0;
}
int element(node *data,int j)
            {
            int i;
            for(i=1;i<=j-1;i++)
                        data=data->next;
            return(data->item);
            }

No comments:

Post a Comment