Skip to content

Commit c326c5f

Browse files
committed
how can i upgrade my efficency game :(
1 parent 3d9bc90 commit c326c5f

6 files changed

+207
-18
lines changed

charFreq.c

+1-7
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,9 @@ int main()
7070
{
7171
freq++;
7272
}
73-
}//
73+
}
7474
ch[i] = freq;
7575
debug printf("[%c] [%d]\n",(char)i,freq);
76-
// if (ch[(int)str[i]] == 0) //adds frequency values to the correspondent ascii index
77-
// {
78-
// //TODO #3 it's not computing the char ' ' or ',' or '.'
79-
// ch[(int)str[i]] = freq;
80-
// debug printf("\t\tch[%d] = %d\n",(int)str[i],ch[(int)str[i]]);
81-
// }
8276
}
8377

8478
for (int aux = 0; aux < 256 ; aux++)

crescent_list.c

+2-9
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ int is_empty(NODE *head)
2727
{
2828
return (head == NULL);
2929
}
30-
NODE* bubbleSort(NODE* head, int num)
30+
NODE* bubbleSort(NODE* head)
3131
{
3232
NODE* pos;
3333
NODE* tmp = NULL;
@@ -43,12 +43,6 @@ NODE* bubbleSort(NODE* head, int num)
4343
DEBUG printf("COMEÇAAAAAAAAAAAAA\n");
4444
swap = 0;
4545
pos = head;
46-
47-
// if (pos->next == NULL)
48-
// {
49-
// printf("FIM\n");
50-
// continue;
51-
// }
5246
DEBUG printf("o proximo nao é nulo\n");
5347

5448
while(pos->next != tmp)
@@ -100,8 +94,7 @@ int main()
10094
DEBUG printf("Elemento %d adicionado\n",n);
10195
i++;
10296
}
103-
order(lista, i);
104-
//bubbleSort(lista);
97+
bubbleSort(lista);
10598
printer(lista);
10699
return 0;
107100
}

intersection.c

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <stdbool.h>
5+
#define DEBUG if(0)
6+
typedef struct node
7+
{
8+
int data;
9+
struct node* next;
10+
}NODE;
11+
12+
typedef struct head
13+
{
14+
int size;
15+
NODE* head;
16+
NODE* tail;
17+
}LIST;
18+
19+
LIST* init_list()
20+
{
21+
LIST* new_head = (LIST*) malloc(sizeof(LIST));
22+
new_head->head = NULL;
23+
new_head->size = 0;
24+
return new_head;
25+
}
26+
int compar (const void * a, const void * b)
27+
{
28+
return ( *(int*)a - *(int*)b );
29+
}
30+
void add(LIST* list, int data)
31+
{
32+
NODE* new_head = (NODE*) malloc(sizeof(NODE));
33+
new_head->next = list->head;
34+
new_head->data = data;
35+
DEBUG printf("\t[%d]\n",new_head->data);
36+
list->head = new_head;
37+
list->size++;
38+
}
39+
void bubbleSort(LIST* list)
40+
{
41+
NODE* pos; //set the position to head.
42+
NODE* tmp = NULL;
43+
int flag = 0; //flag will switch to 1 while it's making swaps
44+
do
45+
{
46+
pos = list->head;
47+
flag = 0;
48+
49+
while (pos->next != tmp)
50+
{
51+
if (pos->data > pos->next->data)
52+
{
53+
DEBUG printf("%d > %d\n",pos->data , pos->next->data);
54+
int t = pos->data;
55+
pos->data = pos->next->data;
56+
pos->next->data = t;
57+
flag = 1;
58+
}
59+
pos = pos->next;
60+
}
61+
tmp = pos; //stores the last pos 'cus it's already been sorted.
62+
} while (flag);
63+
64+
}
65+
bool search(LIST* list, int target)
66+
{
67+
NODE* tmp = list->head;
68+
while (tmp != NULL)
69+
{
70+
if(tmp->data == target)
71+
{
72+
return true;
73+
}
74+
}
75+
return false;
76+
}
77+
bool comparator(int* a, int* b, LIST* list) //gets two arrays and adds to the list
78+
{
79+
int count = 20;
80+
int sort[20];
81+
int flag = 0;
82+
int tmp;
83+
int t = 0;
84+
int last = 468468486;
85+
DEBUG printf("OI\n");
86+
for (int i = 0; i < count; i++)
87+
{
88+
tmp = a[i];
89+
//DEBUG printf("###### a[%d] = %d #######\n",i, a[i]);
90+
for (int j = 0; j < count; j++)
91+
{
92+
//DEBUG printf("tmp = %d e b[%d] = %d\n", tmp, j, b[j]);
93+
if (tmp == b[j])
94+
{
95+
//DEBUG printf("Elemento [%d] adicionado.\n", tmp);
96+
/* if (search(list, tmp))
97+
{
98+
continue;
99+
}
100+
add(list, tmp);
101+
102+
*/
103+
if (tmp == last)
104+
{
105+
continue;
106+
}
107+
108+
if(i != 0 && tmp == last) continue;
109+
sort[t] = tmp;
110+
last = tmp;
111+
t++;
112+
flag++;
113+
}
114+
}
115+
}
116+
117+
//qsort(sort,t,sizeof(int), compar);
118+
for (int i = 0; i < t; i++)
119+
{
120+
add(list,sort[i]);
121+
}
122+
123+
if (flag > 0)
124+
{
125+
return true;
126+
}
127+
return false;
128+
129+
130+
}
131+
void printer(LIST* head)
132+
{
133+
NODE* tmp = head->head;
134+
while (tmp != NULL)
135+
{
136+
printf("%d\n",tmp->data);
137+
tmp = tmp->next;
138+
}
139+
140+
}
141+
int main()
142+
{
143+
int count = 20;
144+
int n;
145+
int a[count];
146+
int b[count];
147+
LIST* head = init_list();
148+
for (int i = 0; i < count; i++)
149+
{
150+
scanf("%d",&n);
151+
a[i] = n;
152+
}
153+
for (int i = 0; i < count; i++)
154+
{
155+
scanf("%d",&n);
156+
b[i] = n;
157+
}
158+
n = comparator(a,b,head);
159+
bubbleSort(head);
160+
if (n) printer(head);
161+
else printf("VAZIO\n");
162+
}

intersectionv2.c

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <stdbool.h>
5+
#define DEBUG if(1)
6+
typedef struct node
7+
{
8+
char data;
9+
struct node* next;
10+
struct node* previous
11+
}NODE;
12+
13+
typedef struct lista
14+
{
15+
NODE* head;
16+
NODE* tail;
17+
}LIST;
18+
19+
LIST* init_list()
20+
{
21+
LIST* new_head = (LIST*) malloc(sizeof(LIST));
22+
new_head->head = NULL;
23+
new_head->tail = NULL;
24+
return new_head;
25+
}

inverter.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ Node* getLastNode(Node* node)
6262
{
6363
return getLastNode(node->next);
6464
}
65-
}
66-
65+
}
6766
int main()
6867
{
6968
int n;

test.c

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <math.h>
4+
#include <stdlib.h>
5+
6+
int main() {
7+
char n[13];
8+
scanf("%c",&n[2]);
9+
scanf("%c",&n[1]);
10+
scanf("%c",&n[3]);
11+
printf("[%c]\n",n[2]);
12+
printf("[%c]\n",n[1]);
13+
printf("[%c]\n",n[3]);
14+
15+
return 0;
16+
}

0 commit comments

Comments
 (0)