-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamigos.c
118 lines (95 loc) · 2.19 KB
/
amigos.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
/*
3160 - Amigos
typedef struct dataNode
{
char name[30];
} DataNode;
typedef struct node
{
DataNode data;
struct node *next;
} Node;
typedef struct list
{
int size;
Node *head;
} List;
//---- HEADER
List *createList();
void push_front(List *list, DataNode data); // Copia informações para o Topo da lista
void push_back(List *list, DataNode data);
bool isEmpty(List *list); // Verifica se a lista esta vazia
Node *atPos(List *list, int index); // Vai até a posição do Index
//---- FUCTIONS
List *createList()
{
List *list = (List *)malloc(sizeof(List));
list->size = 0;
list->head = NULL;
return list;
}
void push_front(List *list, DataNode data)
{
// Inserindo no Inicio
Node *node = (Node *)malloc(sizeof(Node));
node->data = data;
node->next = list->head;
list->head = node;
list->size++;
}
void printList(List *list, int pos)
{
Node *pointer = atPos(list, pos);
printf("%s\n", pointer->data.name);
}
bool isEmpty(List *list)
{
return (list->size == 0); /* Sai o resultado da comparação */
}
Node *atPos(List *list, int index)
{
if (index >= 0 && index < list->size)
{
Node *node = list->head;
int i;
for (i = 0; i < index; i++)
{
node = node->next; /* para andar para o prox. */
}
return node;
}
return NULL; /* Caso indíce inválido */
}
void insertionSort(List *list)
{
Node *pointer = list->head;
Node *i;
Node *j;
for (i = list->head; i->next != NULL; i = i->next)
{
Node *menor = i;
for (j = i->next; j != NULL; j = j->next)
{
if (strcmp(j->data.name, menor->data.name) < 0)
{
menor = j;
}
}
DataNode aux = i->data;
i->data = menor->data;
menor->data = aux;
}
}
main()
{
List *lista = createList();
DataNode data;
while()
{
scanf("%s", data.name);
push_front(lista, data);
}
insertionSort(lista);
printList(lista, (winstudent - 1));
system("pause");
}