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
/
lista_listas_20.c
87 lines (72 loc) · 1.7 KB
/
lista_listas_20.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
#include<stdio.h>
#include<stdlib.h>
#define bool int
#define true 1
#define false 0
typedef struct nodo{
int n;
struct nodo *next;
}nodo;
void imprime_lista(nodo *head);
nodo* final (nodo *head);
void adiciona(nodo *head,int valor);
nodo* divide_lista(nodo *head,int n);
int main(int argc, char const *argv[])
{
int v;
nodo *head1 = (nodo*) malloc(sizeof(nodo));
head1->next = NULL;//cabeça da lista
nodo *head2 = (nodo*) malloc(sizeof(nodo));
head2->next = NULL;
adiciona(head1,0);
adiciona(head1,1);
adiciona(head1,2);
adiciona(head1,3);
adiciona(head1,4);
adiciona(head1,5);
printf("Dividir em: \n" );
scanf("%d",&v);
head2 = divide_lista(head1,v);
printf("--LISTA 1--\n");
imprime_lista(head1);
printf("--LISTA 2--\n");
imprime_lista(head2);
free(head1);
free(head2);
return 0;
}
void adiciona(nodo *head,int valor){
nodo *novo = (nodo*) malloc(sizeof(nodo));
nodo *fim_lista;
novo->n = valor;
novo->next = NULL;//sempre adiciona no final
fim_lista = final(head);
fim_lista->next = novo;//final é uma função que acha o final da lista que começa em head
}
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");
}
nodo* divide_lista(nodo *head,int n){
nodo *ret = (nodo*) malloc(sizeof(nodo));
nodo *count;
for(count=head->next;count!=NULL;count = count->next){
if(count->n == n){
ret->next = count->next;
count->next = NULL;
printf("--LISTA DIVIDIDA--\n");
return ret;
}
}
printf("Erro ao dividir lista. Tente novamente");
return NULL;
}