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_34.c
85 lines (68 loc) · 1.66 KB
/
lista_listas_34.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
#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 divide(nodo *head1,nodo *head2, nodo *head3);
int main(int argc, char const *argv[])
{
nodo *head1,*head_par,*head_impar;
head1 = (nodo*) malloc(sizeof(nodo));
head_par = (nodo*) malloc(sizeof(nodo));
head_impar = (nodo*) malloc(sizeof(nodo));
adiciona(head1,0);
adiciona(head1,1);
adiciona(head1,2);
adiciona(head1,5);
adiciona(head1,7);
adiciona(head1,21);
adiciona(head1,29);
adiciona(head1,8);
divide(head1,head_par,head_impar);
printf("--LISTA-- : \n");
imprime_lista(head1);
printf("--LISTA PAR-- : \n");
imprime_lista(head_par);
printf("--LISTA ÍMPAR-- : \n");
imprime_lista(head_impar);
free(head);
free(head_par);
free(head_impar);
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 divide(nodo*head1,nodo*head_par,nodo*head_impar){
nodo *count;
for(count = head1->next;count!=NULL;count=count->next){
if(count->n%2==0){
adiciona(head_par,count->n);
}
else{
adiciona(head_impar,count->n);
}
}
}
void imprime_lista(nodo *head){
nodo *count;
printf("[ ");
for(count = head->next;count!=NULL;count=count->next){
printf(" %d ",count->n);
}
printf(" ]\n");
}