-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon elements.cpp
More file actions
78 lines (78 loc) · 1.09 KB
/
common elements.cpp
File metadata and controls
78 lines (78 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
node* createnode(int d)
{
node *nd;
nd=(node*)malloc(sizeof(node));
nd->data=d;
nd->next=NULL;
return nd;
}
node* createlist()
{
int data;char ans;
node *first,*last,*nd;
first=NULL;
while(1)
{
printf("enter data:-");
scanf("%d",&data);
nd= createnode(data);
if(first==NULL)
first=nd;
else
last->next=nd;
last=nd;
printf("do you want to continue(Y\N)?");
fflush(stdin);
scanf("%c",&ans);
if(ans!='y' && ans!='Y')
break;
}
return first;
}
node* common(node *l1,node *l2)
{
node *t1=l1;
node *t2,*l3=NULL,*nd,*last;
while(t1!=NULL)
{
t2=l2;
while(t2!=NULL)
{
if(t1->data==t2->data)
{
nd=createnode(t1->data);
if(l3==NULL)
l3=nd;
else
last->next=nd;
last=nd;
}
t2=t2->next;
}
t1=t1->next;
}
return l3;
}
void printlist(node *first)
{
node *t=first;
while(t!=NULL)
{
printf("%d\t",t->data);
t=t->next;
}
}
int main()
{
node *start3;
start3=common(createlist(),createlist());
printlist(start3);
}