Program code for split up linked list into odd and even value
//Split up two linked list into even and odd value
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct linked
{
int val;
struct linked *next;
}lnk;
lnk *create();
void splitlink(lnk *,lnk **,lnk **);
void disp(lnk *);
void main()
{
lnk *h1=NULL,*h2=NULL,*h;
printf("\n\nEnter the values (0 to exit) : ");
h=create();
printf("\n\nValues in the linked list are : ");
disp(h);
splitlink(h,&h1,&h2);
printf("\n\nValues in the first linked list are : ");
disp(h1);
printf("\n\nValues in the second linked list are : ");
disp(h2);
getch();
}
lnk *create()
{
lnk *temp,*ptr,*h=NULL;
int v;
while(1)
{
scanf("%d",&v);
if(v==0)
return h;
temp=(lnk*)malloc(sizeof(lnk));
temp->val=v;
temp->next=NULL;
if(h==NULL)
h=temp;
else
ptr->next=temp;
ptr=temp;
}
}
void disp(lnk *h)
{
while(h!=NULL)
{
printf("%d",h->val);
h=h->next;
}
}
void splitlink(lnk *h,lnk **h1,lnk **h2)
{
lnk *temp1,*ptr1,*temp2,*ptr2;
while(h!=NULL)
{
if(h->val%2==0)
{
temp1=h;
if(*h1==NULL)
{
*h1=temp1;
}
else
ptr1->next=temp1;
ptr1=temp1;
}
else
{
temp2=h;
if(*h2==NULL)
{
*h2=temp2;
}
else
ptr2->next=temp2;
ptr2=temp2;
}
h=h->next;
}
ptr1->next=NULL;
ptr2->next=NULL;
}
I/O :
Enter the values (0 to exit) : 1 2 3 4 5 6 7 8 9
Values in first linked list are : 2 4 6 8
Values in first linked list are : 1 3 5 7 9
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct linked
{
int val;
struct linked *next;
}lnk;
lnk *create();
void splitlink(lnk *,lnk **,lnk **);
void disp(lnk *);
void main()
{
lnk *h1=NULL,*h2=NULL,*h;
printf("\n\nEnter the values (0 to exit) : ");
h=create();
printf("\n\nValues in the linked list are : ");
disp(h);
splitlink(h,&h1,&h2);
printf("\n\nValues in the first linked list are : ");
disp(h1);
printf("\n\nValues in the second linked list are : ");
disp(h2);
getch();
}
lnk *create()
{
lnk *temp,*ptr,*h=NULL;
int v;
while(1)
{
scanf("%d",&v);
if(v==0)
return h;
temp=(lnk*)malloc(sizeof(lnk));
temp->val=v;
temp->next=NULL;
if(h==NULL)
h=temp;
else
ptr->next=temp;
ptr=temp;
}
}
void disp(lnk *h)
{
while(h!=NULL)
{
printf("%d",h->val);
h=h->next;
}
}
void splitlink(lnk *h,lnk **h1,lnk **h2)
{
lnk *temp1,*ptr1,*temp2,*ptr2;
while(h!=NULL)
{
if(h->val%2==0)
{
temp1=h;
if(*h1==NULL)
{
*h1=temp1;
}
else
ptr1->next=temp1;
ptr1=temp1;
}
else
{
temp2=h;
if(*h2==NULL)
{
*h2=temp2;
}
else
ptr2->next=temp2;
ptr2=temp2;
}
h=h->next;
}
ptr1->next=NULL;
ptr2->next=NULL;
}
I/O :
Enter the values (0 to exit) : 1 2 3 4 5 6 7 8 9
Values in first linked list are : 2 4 6 8
Values in first linked list are : 1 3 5 7 9
Comments
Post a Comment