S3-2
Write a program in ‘C’ language that accepts two singly linked lists A and B as
input.
Now, print a singly linked list that consists of only those elements,
which are common to both A and B.
# include<stdio.h>
# include<conio.h>
struct student
{
int rollno;
struct student *next;
} *s1, *s2;
/*creation of linked list*/
void list_create (struct student *s1)
{
printf ("enter roll number(-1 to stop)") ;
scanf("%d", & s1->rollno);
if (s1->rollno != -1) /* ‘-1’ is entered to stop*/
{
s1->next = (struct student*) malloc (sizeof (struct student));
list_create (s1->next);
}
else
s1->next = NULL;
return;
}
/*display the list*/
void display_list (struct student *s1)
{
if (s1->next != NULL)
{
printf ("%dn", s1->rollno);
display_list (s1->next);
}
}
/* function to print common elements */
void common_elements (struct student *s1, struct student * s2)
{
int flag = 0;
struct student *temp = s2;
/* temp is to store the initial node of s2*)*/
printf ("common elements n");
while (s1->next !=NULL) /*repeat till the end of s1*/
{
while (s2->next !=NULL)/* This loop is repeated that many times the number of
nodes in s1x s2
*/
{
if (s1->rollno==s2->rollno)
{
flag=1;
printf ("%dn", s1->rollno);
/*flag is set to show that an equal roll number is met*/
Page 1
S3-2
}
s2=s2->next;
}
s1=s1->next;
s2=temp; /* s2 is to be started again from beginning when s1=s1®next */
}
if (flag==0)
printf ("no common elements");
}
void main ()
{
s1=(struct student*) malloc (sizeof (struct student));
s2=(struct student*) malloc (sizeof (struct student));
printf("Enter the elements to first list:n");
list_create (s1);
printf ("elements in first list n");
display_list(s1);
getch();
printf("enter elements to second listn");
list_create (s2); /* creating second list */
printf("elements in second list n");
display_list (s2);
/*printing common elements */
common_elements(s1, s2);
getch();
}
Page 2

Bcsl 033 data and file structures lab s3-2

  • 1.
    S3-2 Write a programin ‘C’ language that accepts two singly linked lists A and B as input. Now, print a singly linked list that consists of only those elements, which are common to both A and B. # include<stdio.h> # include<conio.h> struct student { int rollno; struct student *next; } *s1, *s2; /*creation of linked list*/ void list_create (struct student *s1) { printf ("enter roll number(-1 to stop)") ; scanf("%d", & s1->rollno); if (s1->rollno != -1) /* ‘-1’ is entered to stop*/ { s1->next = (struct student*) malloc (sizeof (struct student)); list_create (s1->next); } else s1->next = NULL; return; } /*display the list*/ void display_list (struct student *s1) { if (s1->next != NULL) { printf ("%dn", s1->rollno); display_list (s1->next); } } /* function to print common elements */ void common_elements (struct student *s1, struct student * s2) { int flag = 0; struct student *temp = s2; /* temp is to store the initial node of s2*)*/ printf ("common elements n"); while (s1->next !=NULL) /*repeat till the end of s1*/ { while (s2->next !=NULL)/* This loop is repeated that many times the number of nodes in s1x s2 */ { if (s1->rollno==s2->rollno) { flag=1; printf ("%dn", s1->rollno); /*flag is set to show that an equal roll number is met*/ Page 1
  • 2.
    S3-2 } s2=s2->next; } s1=s1->next; s2=temp; /* s2is to be started again from beginning when s1=s1®next */ } if (flag==0) printf ("no common elements"); } void main () { s1=(struct student*) malloc (sizeof (struct student)); s2=(struct student*) malloc (sizeof (struct student)); printf("Enter the elements to first list:n"); list_create (s1); printf ("elements in first list n"); display_list(s1); getch(); printf("enter elements to second listn"); list_create (s2); /* creating second list */ printf("elements in second list n"); display_list (s2); /*printing common elements */ common_elements(s1, s2); getch(); } Page 2