Skip to content

Commit 1b04081

Browse files
committed
Update problem23.c
1 parent 0112080 commit 1b04081

File tree

1 file changed

+268
-0
lines changed

1 file changed

+268
-0
lines changed

problem23.c

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,274 @@
22
Maneesha, Dept of ECE
33
*/
44

5+
//Only intersection
6+
#include <stdio.h>
7+
8+
#include <stdlib.h>
9+
10+
 
11+
12+
struct node
13+
14+
{
15+
16+
int num;
17+
18+
struct node *next;
19+
20+
};
21+
22+
void create(struct node **);
23+
24+
void findintersect(struct node *, struct node *, struct node **);
25+
26+
void display(struct node *);
27+
28+
void release(struct node **);
29+
30+
 
31+
32+
int main()
33+
34+
{
35+
36+
struct node *phead, *qhead, *intersect, *unionlist;
37+
38+
 
39+
40+
phead = qhead = intersect = unionlist = NULL;
41+
42+
printf("Enter elements in the list 1\n");
43+
44+
create(&phead);
45+
46+
printf("\nEnter elements in the list 2\n");
47+
48+
create(&qhead);
49+
50+
findunion(phead, qhead, &unionlist);
51+
52+
findintersect(phead, qhead, &intersect);
53+
54+
printf("\nDisplaying list 1:\n");
55+
56+
display(phead);
57+
58+
printf("Displaying list 2:\n");
59+
60+
display(qhead);
61+
62+
printf("Displaying the intersection of the 2 lists:\n");
63+
64+
if (intersect == NULL)
65+
66+
{
67+
68+
printf("Null\n");
69+
70+
}
71+
72+
else
73+
74+
{
75+
76+
display(intersect);
77+
78+
}
79+
80+
release(&phead);
81+
82+
release(&qhead);
83+
84+
release(&unionlist);
85+
86+
release(&intersect);
87+
88+
 
89+
90+
return 0;
91+
92+
}
93+
94+
 
95+
96+
void findintersect(struct node *p, struct node *q, struct node **intersect)
97+
98+
{
99+
100+
struct node *ptemp, *qtemp, *itemp, *irear, *ifront;
101+
102+
 
103+
104+
ptemp = p;
105+
106+
while (ptemp != NULL)
107+
108+
{
109+
110+
qtemp = q;
111+
112+
ifront = *intersect;
113+
114+
while (qtemp != NULL && ptemp->num != qtemp->num)
115+
116+
{
117+
118+
qtemp = qtemp->next;
119+
120+
}
121+
122+
if (qtemp != NULL)
123+
124+
{
125+
126+
if (ifront != NULL)
127+
128+
{
129+
130+
if (ifront->num == qtemp->num)
131+
132+
{
133+
134+
ptemp = ptemp->next;
135+
136+
continue;
137+
138+
}
139+
140+
ifront = ifront->next;
141+
142+
}
143+
144+
itemp = (struct node *)malloc(sizeof(struct node));
145+
146+
itemp->num = qtemp->num;
147+
148+
itemp->next = NULL;
149+
150+
if (*intersect == NULL)
151+
152+
{
153+
154+
*intersect = itemp;
155+
156+
}
157+
158+
else
159+
160+
{
161+
162+
irear->next = itemp;
163+
164+
}
165+
166+
irear = itemp;
167+
168+
}
169+
170+
ptemp = ptemp->next;
171+
172+
}
173+
174+
}
175+
176+
 
177+
178+
 
179+
180+
void create(struct node **head)
181+
182+
{
183+
184+
struct node *temp, *rear;
185+
186+
int ch, a;
187+
188+
 
189+
190+
do
191+
192+
{
193+
194+
printf("Enter a number: ");
195+
196+
scanf("%d", &a);
197+
198+
temp = (struct node *)malloc(sizeof(struct node));
199+
200+
temp->num = a;
201+
202+
temp->next = NULL;
203+
204+
if (*head == NULL)
205+
206+
{
207+
208+
*head = temp;
209+
210+
}
211+
212+
else
213+
214+
{
215+
216+
rear->next = temp;
217+
218+
}
219+
220+
rear = temp;
221+
222+
printf("Do you want to continue [1/0] ? ");
223+
224+
scanf("%d", &ch);
225+
226+
} while (ch != 0);
227+
228+
}
229+
230+
 
231+
232+
void display(struct node *head)
233+
234+
{
235+
236+
while (head != NULL)
237+
238+
{
239+
240+
printf("%d", head->num);
241+
242+
head = head->next;
243+
244+
}
245+
246+
printf("\n");
247+
248+
}
249+
250+
 
251+
252+
void release(struct node **head)
253+
254+
{
255+
256+
struct node *temp = *head;
257+
258+
while ((*head) != NULL)
259+
260+
{
261+
262+
(*head) = (*head)->next;
263+
264+
free (temp);
265+
266+
temp = *head;
267+
268+
}
269+
270+
}
271+
272+
//intersection and union
5273
#include <stdio.h>
6274

7275
#include <stdlib.h>

0 commit comments

Comments
 (0)