QUES=> Printing the common elements in two given linked
lists.
CODE
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
typedef struct nodetype
{ int info;
struct nodetype *next;
}node;
void common (node *head1, node *head2)
{ node *p;
p=head2;
printf("\n common elements are :");
while(head2!=NULL || head1!=NULL)
{
head2=p;
while(head2!=NULL)
{
if(head1->info==head2->info)
printf(" %d ",head1->info);
head2=head2->next;
}
head1=head1->next;
}
}
void main()
{ node *head1,*head2,*ptr1,*ptr2,*p,*q,*h1,*h2;
int i,j;
clrscr();
ptr1=(node*)malloc(sizeof(node));
ptr2=(node*)malloc(sizeof(node));
head1=ptr1;
head2=ptr2;
h1=ptr1;
h2=ptr2;
ptr1->info=1;
ptr2->info=7;
for(i=2,j=2; i<=6; i++,j=j+2)
{ p=(node*)malloc(sizeof(node));
q=(node*)malloc(sizeof(node));
p->info=i;
q->info=j;
ptr1->next=p;
ptr1=ptr1->next ;
ptr2->next=q;
ptr2=ptr2->next ;
}
ptr1->next=NULL;
ptr2->next=NULL;
printf("The link list is : ");
while(head1!=NULL)
{ printf(" %d ",head1->info);
head1=head1->next;
}
printf("\nThe link list is : ");
while(head2!=NULL)
{ printf(" %d ",head2->info);
head2=head2->next;
}
common(h1,h2);
getch();
}
OUTPUT
CODE
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
typedef struct nodetype
{ int info;
struct nodetype *next;
}node;
void common (node *head1, node *head2)
{ node *p;
p=head2;
printf("\n common elements are :");
while(head2!=NULL || head1!=NULL)
{
head2=p;
while(head2!=NULL)
{
if(head1->info==head2->info)
printf(" %d ",head1->info);
head2=head2->next;
}
head1=head1->next;
}
}
void main()
{ node *head1,*head2,*ptr1,*ptr2,*p,*q,*h1,*h2;
int i,j;
clrscr();
ptr1=(node*)malloc(sizeof(node));
ptr2=(node*)malloc(sizeof(node));
head1=ptr1;
head2=ptr2;
h1=ptr1;
h2=ptr2;
ptr1->info=1;
ptr2->info=7;
for(i=2,j=2; i<=6; i++,j=j+2)
{ p=(node*)malloc(sizeof(node));
q=(node*)malloc(sizeof(node));
p->info=i;
q->info=j;
ptr1->next=p;
ptr1=ptr1->next ;
ptr2->next=q;
ptr2=ptr2->next ;
}
ptr1->next=NULL;
ptr2->next=NULL;
printf("The link list is : ");
while(head1!=NULL)
{ printf(" %d ",head1->info);
head1=head1->next;
}
printf("\nThe link list is : ");
while(head2!=NULL)
{ printf(" %d ",head2->info);
head2=head2->next;
}
common(h1,h2);
getch();
}
OUTPUT
No comments:
Post a Comment