-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintersection.c
152 lines (147 loc) · 3.24 KB
/
intersection.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define DEBUG if(0)
typedef struct node
{
int data;
struct node* next;
}NODE;
typedef struct head
{
int size;
NODE* head;
}LIST;
LIST* init_list()
{
LIST* new_head = (LIST*) malloc(sizeof(LIST));
new_head->head = NULL;
new_head->size = 0;
return new_head;
}
int compar (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
void add(LIST* list, int data)
{
NODE* new_head = (NODE*) malloc(sizeof(NODE));
new_head->next = list->head;
new_head->data = data;
DEBUG printf("\t[%d]\n",new_head->data);
list->head = new_head;
list->size++;
}
void bubbleSort(LIST* list)
{
NODE* pos; //auxiliary node* that points to head
NODE* tmp = NULL;
int flag = 0; //flag will switch to 1 while it's making swaps
do
{
pos = list->head;//set the position to head.
flag = 0;
while (pos->next != tmp)
{
if (pos->data > pos->next->data)
{
DEBUG printf("%d > %d\n",pos->data , pos->next->data);
int t = pos->data;
pos->data = pos->next->data;
pos->next->data = t;
flag = 1;
}
pos = pos->next;
}
tmp = pos; //stores the last pos 'cus it's already been sorted.
} while (flag);
}
bool search(LIST* list, int target)
{
NODE* tmp = list->head;
while (tmp != NULL)
{
if(tmp->data == target)
{
return true;
}
tmp = tmp->next;
}
return false;
}
bool comparator(int* a, int* b, LIST* list) //gets two arrays and adds to the list
{
int count = 20;
int sort[20];
int flag = 0;
int tmp;
int t = 0;
int last = 468468486;
for (int i = 0; i < count; i++)
{
tmp = a[i];
for (int j = 0; j < count; j++)
{
DEBUG printf("OI [%d]\n", j);
if (tmp == b[j])
{
if (tmp == last) continue;
if(i != 0 && tmp == last) continue;
sort[t] = tmp;
last = tmp;
t++;
flag++;
}
}
}
DEBUG printf("XAU\n");
if (flag == 0) return false;
for (int i = 0; i < t; i++)
{
tmp = sort[i];
DEBUG printf("tMP = [%d]\n", tmp);
if (search(list, tmp) && i != 0)
{
continue;
}
add(list,sort[i]);
}
return true;
}
void printer(LIST* head)
{
NODE* tmp = head->head;
while (tmp != NULL)
{
printf("%d\n",tmp->data);
tmp = tmp->next;
}
}
int main()
{
int count = 20;
int n;
int a[count];
int b[count];
LIST* head = init_list();
for (int i = 0; i < count; i++)
{
scanf("%d",&n);
a[i] = n;
DEBUG printf("a[%d] = %d\n",i,n);
}
for (int i = 0; i < count; i++)
{
scanf("%d",&n);
b[i] = n;
DEBUG printf("b[%d] = %d\n",i,n);
}
DEBUG printf("CADE CADE VAI PRO COMPARADOR OU NAO\n");
n = comparator(a,b,head);
if(!n) {printf("VAZIO\n");return 0;}
bubbleSort(head);
if (n) printer(head);
else
return 0;
}