-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrescent_list.c
100 lines (89 loc) · 1.89 KB
/
crescent_list.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
//https://thehuxley.com/problem/266?quizId=6096
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define DEBUG if (0)
#define MAX 100001
typedef struct node
{
int item;
struct node *next;
}NODE;
NODE * create_list()
{
return NULL;
}
NODE *add(NODE *head, int item)
{
NODE *new_node = (NODE *)malloc(sizeof(NODE));
new_node->item = item;
new_node->next = head;
return new_node;
}
int is_empty(NODE *head)
{
return (head == NULL);
}
NODE* bubbleSort(NODE* head)
{
NODE* pos;
NODE* tmp = NULL;
int swap,t;
if (is_empty(head))
{
return NULL;
}
do
{
DEBUG printf("COMEÇAAAAAAAAAAAAA\n");
swap = 0;
pos = head;
DEBUG printf("o proximo nao é nulo\n");
while(pos->next != tmp)
{
if(pos->item > pos->next->item)
{
//tmp = pos; // isso aqui copia os structs ou só copia os endereços?
DEBUG printf("%d > %d\n",pos->item , pos->next->item);
t = pos->item;
pos->item = pos->next->item;
pos->next->item = t;
swap = 1;
DEBUG printf("swap rolando \t");
}
pos = pos->next;
DEBUG printf("analisando o proximo\n");
}
tmp = pos;
}while(swap);
return pos;
}
void printer(NODE *head)
{
if (head == NULL)
{
DEBUG printf("Lista vazia!\n");
}
while (head != NULL)
{
printf("[%d]->", head->item);
head = head->next;
}
printf("\n");
DEBUG printf("\nFim da Lista!\n");
}
int main()
{
NODE* lista = create_list();
int n;
int i = 0;
while (scanf("%d",&n)!= EOF)
{
lista = add(lista,n);
DEBUG printf("Elemento %d adicionado\n",n);
i++;
}
bubbleSort(lista);
printer(lista);
return 0;
}