/
main.c
625 lines (494 loc) · 12.3 KB
/
main.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
#include "ourLib.h"
int main(){
int fn,size;
TpContato *tp = start();
ListaContato *lista = init(), *lista_copy=init(), *lista_aux=init();
TpContato *aux = start(), *copy = aux;
clock_t cinicio, cfinal;
system("clear");
do{
printf(
"Escolha uma das opções abaixo:\n"
"\t1) Criar Lista\n"
"\t2) Cirar Vetor\n"
"\t3) Ordenacao com Comb Sort\n"
"\t4) Ordenacao com Merge Sort\n"
"\t5) Sair\n\n"
);
scanf(" %d",&fn);
switch(fn){
case 1:
system("clear");
printf("Criar lista com quantos elementos?\n");
scanf(" %d", &size);
lista = createLista(size);
printLista(lista);
break;
case 2:
system("clear");
printf("\t2) Cirar Vetor\n");
tp = createVetor();
fillVetor(tp);
printVetor(tp);
break;
case 3:
system("clear");
printf("\t3) Ordenacao com Comb Sort\n");
if (!isEmpty_vetor(tp)){
copy = createVetor();
vetcpy(copy, tp);
cinicio = clock();
combSort_vetor(copy);
cfinal = clock();
printf("\n");
printVetor(copy);
printf("Tempo: %f segundos.\n", (float)(cfinal-cinicio)/CLOCKS_PER_SEC);
if(!isEmpty_vetor(copy)) free(copy);
}
if (!isEmpty_lista(lista)){
lista_copy = listacpy(lista);
cinicio = clock();
combSort_lista(lista_copy,size);
cfinal = clock();
printf("\n");
printLista(lista_copy);
printf("Tempo: %f segundos.\n", (float)(cfinal-cinicio)/CLOCKS_PER_SEC);
lista_copy = ourFree(lista_copy);
}
break;
case 4:
system("clear");
printf("\t4) Ordenacao com Merge Sort\n");
if (!isEmpty_vetor(tp)){
aux = createVetor();
copy = createVetor();
vetcpy(copy, tp);
cinicio = clock();
mergeSort(copy, 0, MAX-1, aux); // Vetor vai de 0-39
cfinal = clock();
printf("\n");
printVetor(copy);
printf("Tempo: %f segundos.\n", (float)(cfinal-cinicio)/CLOCKS_PER_SEC);
if (!isEmpty_vetor(aux)) free(aux); // Liberando memória
if (!isEmpty_vetor(copy)) free(copy); // Liberando memória
}
if (!isEmpty_lista(lista)){
lista_copy = listacpy(lista);
cinicio = clock();
lista_aux = mergeSortList(lista_copy, 0, size-1, lista_aux);
cfinal = clock();
printf("\n");
printLista(lista_copy);
printf("Tempo: %f segundos.\n", (float)(cfinal-cinicio)/CLOCKS_PER_SEC);
lista_copy = ourFree(lista_copy);
lista_aux = ourFree(lista_aux);
}
break;
case 5:
system("clear");
printf("BYE!\n\n");
break;
default:
system("clear");
printf("Opção invalida!\n");
break;
}
}while(fn != 5);
if (!isEmpty_vetor(tp)) free(tp);
lista = ourFree(lista);
return 0;
}
TpContato *createVetor(){
return (TpContato *) malloc(sizeof(TpContato) * MAX);
}
TpContato *start(){
return NULL;
}
void fillVetor(TpContato *tp){
int i, j, num[3], fone[8];
for(i=0; i < MAX; i++){
strcpy(tp[i].nome, "Fulano "); // Escrevendo "Fulano "
strcpy(tp[i].fone, ""); // Inicializano fone
for(j=0; j<3; j++) { // Numero para concatenar com Fulano
num[j] = rand()%9; // Aumentar a 'aleatoriedade' dos numeros, mantendo em 3 digitos
sprintf(tp[i].nome, "%s%d", tp[i].nome, num[j]); // Peguei na net essa coisa.. funcionou hahaha ---> Fulano 'num'
}
for(j=0; j<8; j++){
fone[j] = rand()%9; // Aumentar a 'aleatoriedade' dos numeros, mantendo em 8 digitos
sprintf(tp[i].fone, "%s%d", tp[i].fone, fone[j]); // Agora o telefone
}
}
}
int isEmpty_vetor(TpContato *head){
if (head == NULL){
return 1;
}else {
return 0;
}
}
void printVetor(TpContato *tp){
int i;
printf("\nVetor:\n\n");
for(i=0; i < MAX; i++){
printf("-----------------\n");
printf("Nome: %s\nFone: %s\n", tp[i].nome, tp[i].fone);
}
printf("-----------------\n\n");
}
void vetcpy(TpContato *dest, TpContato *src){
int i=0; //Copiando os valores de SRC para DEST
while(i < MAX){
strcpy(dest[i].nome, src[i].nome);
strcpy(dest[i].fone, src[i].fone);
i++;
}
}
void combSort_vetor(TpContato* tp){
int gap = MAX,
swap = FALSE,
i, j;
TpContato aux;
while(gap > 1 || swap == TRUE){
gap = (int) gap / 1.3;
if (gap < 1) gap = 1;
swap = FALSE;
for(i=0, j=gap; j < MAX; i++, j++){
if (strcmp(tp[i].nome,tp[j].nome) > 0){
aux = tp[i];
tp[i] = tp[j];
tp[j] = aux;
swap = TRUE;
}
}
}
}
void mergeArray(TpContato *h,int begin,int mid,int end, TpContato *temp){
int i = begin,j = mid + 1;
int m = mid,n = end;
int k = 0; //contador do temporario
while(i <= m && j <= n){ //Entra para fazer a comparação
if(strcmp(h[i].nome, h[j].nome) <= 0){ //Se o 1° <= 2°
strcpy(temp[k].nome, h[i].nome); //Copia o primeiro
strcpy(temp[k++].fone, h[i++].fone);
}
else{ //Senao
strcpy(temp[k].nome, h[j].nome); //Copia o segundo
strcpy(temp[k++].fone, h[j++].fone); //Passa o segundo pra frente.
}
}
while(i <= m){ //Caso nao tenha sido feito alterações antes do meio
strcpy(temp[k].nome, h[i].nome); //Copiar eles do jeito que estao
strcpy(temp[k++].fone, h[i++].fone);
}
while(j <= n){ //Caso nao tenha sido feito alterações depois do meio
strcpy(temp[k].nome, h[j].nome); //Copiar eles do jeito que estao
strcpy(temp[k++].fone, h[j++].fone);
}
for(i = 0;i < k;i++){ //Salvando do temp no titular
strcpy(h[begin + i].nome, temp[i].nome);
strcpy(h[begin + i].fone, temp[i].fone);
}
}
void mergeSort(TpContato *h,int begin,int end, TpContato *temp){
if(begin < end){
int mid = (begin + end) / 2;
mergeSort(h,begin,mid,temp);
mergeSort(h,mid + 1,end,temp);
mergeArray(h,begin,mid,end,temp);
}
}
// Funções da Lista
ListaContato *init(){
return NULL;
}
ListaContato *createLista(int tam){
int i, j, num[3], fone_num[8];
char nome[40], fone[30];
ListaContato *lista = init();
for(i=0; i < tam; i++){
strcpy(nome, "Fulano "); // Escrevendo "Fulano "
strcpy(fone, ""); // Inicializano fone
for(j=0; j<3; j++) { // Numero para concatenar com Fulano
num[j] = rand()%9; // Aumentar a 'aleatoriedade' dos numeros, mantendo em 3 digitos
sprintf(nome, "%s%d", nome, num[j]); // Peguei na net essa coisa.. funcionou hahaha ---> Fulano 'num'
}
for(j=0; j<8; j++){
fone_num[j] = rand()%9; // Aumentar a 'aleatoriedade' dos numeros, mantendo em 8 digitos
sprintf(fone, "%s%d", fone, fone_num[j]); // Agora o telefone
}
lista = push(lista,nome,fone);
}
return lista;
}
ListaContato *anotherPush(char nome[], char fone[]){
ListaContato *novo = (ListaContato *) malloc(sizeof(ListaContato));
strcpy(novo->contato.nome,nome);
strcpy(novo->contato.fone,fone);
novo->prev = NULL;
novo->next = NULL;
return novo;
}
ListaContato *push(ListaContato* head, char nome[], char fone[]){
ListaContato *this=head,
*previous=init(),
*novo = init();
novo = (ListaContato *) malloc(sizeof(ListaContato));
strcpy(novo->contato.nome,nome);
strcpy(novo->contato.fone,fone);
while(this!=NULL){
previous = this;
this = this->next;
}
if (previous != NULL){
if (previous->next != NULL){
novo->prev = previous;
novo->next = previous->next;
previous->next = novo;
return head;
}else{
novo->prev = previous;
novo->next = init();
previous->next = novo;
return head;
}
}else if (this==head){
novo->next = head;
novo->prev = init();
return novo;
}
return head;
}
ListaContato *pop(ListaContato* head, TpContato contato){
ListaContato *pop = init(),
*previous = init(),
*next = init();
pop = find(head,contato);
if (pop==NULL){
return head;
}
previous = pop->prev;
next = pop->next;
if (previous == NULL){
if (next == NULL) {
head=init();
}else{
next->prev = init();
head=next;
}
}else{
if (next == NULL) {
previous->next = NULL;
}else{
previous->next = next;
next->prev = previous;
}
}
free(pop);
return head;
}
int isEmpty_lista(ListaContato *head){
if (head == NULL){
return 1;
}else {
return 0;
}
}
ListaContato *find(ListaContato* head, TpContato contato){
ListaContato *p = init();
for(p=head; p!=NULL; p=p->next){
if( (strcmp(p->contato.nome,contato.nome) == 0) && (strcmp(p->contato.fone,contato.fone) == 0) ){
return p;
}
}
return NULL; //Elemento não está na lista
}
void printLista(ListaContato *h){
ListaContato* p=init();
printf("\nLista:\n\n");
for(p=h; p!=NULL; p=p->next){
printf("-----------------\n");
printf("Nome: %s\nFone: %s\n", p->contato.nome, p->contato.fone);
}
printf("-----------------\n\n");
}
void combSort_lista(ListaContato* head, int tam){
int gap = tam,
swap = FALSE,
n;
ListaContato *i = init(),
*j = init();
TpContato aux;
while(gap > 1 || swap == TRUE){
gap = (int) gap / 1.3;
if (gap < 1) gap = 1;
swap = FALSE;
i = head;
j = i;
for (n=0;n<gap;n++){
j=j->next;
}
for(; j!=NULL; i=i->next, j=j->next){
if (strcmp(i->contato.nome,j->contato.nome) > 0){
aux = i->contato;
i->contato = j->contato;
j->contato = aux;
swap = TRUE;
}
}
}
}
ListaContato *listacpy(ListaContato *src){
ListaContato *dest = init();
char nome[40],fone[30];
while(src != NULL){
strcpy(nome, src->contato.nome);
strcpy(fone, src->contato.fone);
dest = push(dest, nome, fone);
src = src->next;
}
return dest;
}
ListaContato *backToStart(ListaContato *head){
while(head->prev != NULL)
head = head->prev;
return head;
}
ListaContato *runTo(ListaContato *head, int find){
int i=0;
while(i < find){
head = head->next;
i++;
}
return head;
}
ListaContato *mergeSortList(ListaContato *head, int begin, int end, ListaContato *aux){
if(begin < end){
int mid = (begin + end) / 2;
aux = mergeSortList(head,begin,mid,aux);
aux = mergeSortList(head,mid + 1,end,aux);
aux = mergeList(head,begin,mid,end,aux);
}
return aux;
}
ListaContato *mergeList(ListaContato *head, int begin, int mid, int end, ListaContato *aux){
int i=begin, j=mid+1, m=mid, n=end, k=0;
ListaContato *li = init(),
*lj = init(),
*h = aux;
char nome[40],fone[30];
while(i <= m && j <= n){
li = runTo(head,i);
lj = runTo(head,j);
if(strcmp(li->contato.nome, lj->contato.nome) <= 0 ){
strcpy(nome, li->contato.nome);
strcpy(fone, li->contato.fone);
if(aux == NULL){
aux = push(aux, nome, fone);
h = aux;
}
else{
h = runTo(aux, k);
if(h == NULL){
h = runTo(aux, k-1);
h->next = anotherPush(nome, fone);
h->next->prev = h;
h = h->next;
}
else{
strcpy(h->contato.nome, nome);
strcpy(h->contato.fone, fone);
}
}
i++;
k++;
}
else{
strcpy(nome, lj->contato.nome);
strcpy(fone, lj->contato.fone);
if(aux == NULL){
aux = push(aux, nome, fone);
h = aux;
}
else{
h = runTo(aux, k);
if(h == NULL){
h = runTo(aux, k-1);
h->next = anotherPush(nome, fone);
h->next->prev = h;
h = h->next;
}
else{
strcpy(h->contato.nome, nome);
strcpy(h->contato.fone, fone);
}
}
j++;
k++;
}
}
li = runTo(head, i);
while(i <= m){
strcpy(nome, li->contato.nome);
strcpy(fone, li->contato.fone);
if(aux == NULL){
aux = push(aux, nome, fone);
h = aux;
}
else{
h = runTo(aux, k);
if(h == NULL){
h = runTo(aux, k-1);
h->next = anotherPush(nome, fone);
h->next->prev = h;
h = h->next;
}
else{
strcpy(h->contato.nome, nome);
strcpy(h->contato.fone, fone);
}
}
i++;
k++;
li = li->next;
}
lj = runTo(head, j);
while(j <= n){
strcpy(nome, lj->contato.nome);
strcpy(fone, lj->contato.fone);
if(aux == NULL){
aux = push(aux, nome, fone);
h = aux;
}
else{
h = runTo(aux, k);
if(h == NULL){
h = runTo(aux, k-1);
h->next = anotherPush(nome, fone);
h->next->prev = h;
h = h->next;
}
else{
strcpy(h->contato.nome, nome);
strcpy(h->contato.fone, fone);
}
}
j++;
k++;
lj = lj->next;
}
head = runTo(head, begin);
for(i = 0; i < k; i++){
strcpy(head->contato.nome, aux->contato.nome);
strcpy(head->contato.fone, aux->contato.fone);
if(aux->next != NULL)
aux = aux->next;
head = head->next;
}
return backToStart(aux);
}
ListaContato *ourFree(ListaContato *lista){
while (!isEmpty_lista(lista)){
lista = pop(lista, lista->contato);
}
return NULL;
}