-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathaddition2.c
111 lines (107 loc) · 1.98 KB
/
addition2.c
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
} *first1 = NULL, *first2 = NULL, *inter = NULL, *lasti = NULL;
void create1(int a[], int n)
{
struct node *p, *last;
first1 = (struct node *)malloc(sizeof(struct node));
first1->data = a[0];
first1->next = NULL;
last = first1;
for (int i = 1; i < n; i++)
{
p = (struct node *)malloc(sizeof(struct node));
p->data = a[i];
p->next = NULL;
last->next = p;
last = p;
}
}
void create2(int a[], int n)
{
struct node *p, *last;
first2 = (struct node *)malloc(sizeof(struct node));
first2->data = a[0];
first2->next = NULL;
last = first2;
for (int i = 1; i < n; i++)
{
p = (struct node *)malloc(sizeof(struct node));
p->data = a[i];
p->next = NULL;
last->next = p;
last = p;
}
}
void push(int value)
{
struct node *p;
p = (struct node *)malloc(sizeof(struct node));
p->data = value;
p->next = NULL;
if (inter == NULL)
{
inter = p;
lasti = p;
}
else
{
lasti->next = p;
lasti = p;
}
}
void intersect(struct node *p, struct node *q)
{
while (p != NULL && q != NULL)
{
if (p->data > q->data)
{
push(q->data);
q = q->next;
}
else
{
push(p->data);
p = p->next;
}
}
if (p == NULL)
{
while (q != NULL)
{
push(q->data);
q = q->next;
}
}
else
{
while (p != NULL)
{
push(p->data);
p = p->next;
}
}
}
void Display(struct node *p)
{
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
}
int main()
{
int a[] = {1, 3, 5};
int b[] = {2, 4, 6};
create1(a, 3);
create2(b, 3);
intersect(first1, first2);
Display(inter);
return 0;
}