This repository has been archived by the owner on Mar 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlista_listas_33.c
77 lines (62 loc) · 1.48 KB
/
lista_listas_33.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
#include<stdio.h>
#include<stdlib.h>
typedef struct nodo{
int n;
struct nodo *next;
}nodo;
void imprime_lista(nodo *head);
nodo* final (nodo *head);
void adiciona(nodo *head,int valor);
void ordena(nodo *head);
int main(int argc, char const *argv[])
{
nodo *head;
head = (nodo*) malloc(sizeof(nodo));
head->next = NULL;
adiciona(head,3);
adiciona(head,9);
adiciona(head,7);
adiciona(head,1);
adiciona(head,8);
printf("--LISTA-- : \n");
imprime_lista(head);
ordena(head);
printf("--LISTA ORDENADA-- : \n");
imprime_lista(head);
free(head);
return 0;
}
void adiciona(nodo *head,int valor){
nodo *novo = (nodo*) malloc(sizeof(nodo));
nodo *fim_lista;
novo->n = valor;
fim_lista = final(head);
fim_lista->next = novo;//final é uma função que acha o final da lista que começa em head
novo->next = NULL;//sempre adiciona no final
}
nodo* final (nodo *head){
nodo *count;
for(count = head;count->next!=NULL;count=count->next);
return count;
}
void imprime_lista(nodo *head){
nodo *count;
printf("[ ");
for(count = head->next;count!=NULL;count=count->next){
printf(" %d ",count->n);
}
printf(" ]\n");
}
void ordena(nodo *head){
nodo *count1,*count2;
int aux;
for(count1 = head->next;count1!=NULL;count1 = count1->next){
for(count2 = count1;count2!=NULL;count2 = count2->next){
if(count2->n<count1->n){
aux = count1->n;//copio só os conteúdos porque não posso perder a ordem da lista
count1->n = count2->n;
count2->n = aux;
}
}
}
}