-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistas_cabeca.c
More file actions
95 lines (83 loc) · 1.63 KB
/
Copy pathlistas_cabeca.c
File metadata and controls
95 lines (83 loc) · 1.63 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct no {
int chave;
struct no *prox;
} TNo;
TNo *criaLista();
TNo *alocaNo(int k);
void insereLista2(TNo *lista, int k);
void imprimeLista(TNo *p);
void desalocaNo2(TNo *lista);
void main() {
TNo *cabeca = criaLista();
insereLista2(cabeca, 7);
insereLista2(cabeca, 12);
insereLista2(cabeca, 78);
insereLista2(cabeca, 1);
imprimeLista(cabeca->prox);
while(cabeca->prox != NULL) {
desalocaNo2(cabeca);
}
imprimeLista(cabeca->prox);
}
TNo *criaLista() {
TNo *cabeca = NULL;
//cabeca = alocaNo(-1);
cabeca = (TNo *)malloc(sizeof(TNo));
if (cabeca == NULL) {
return NULL;
}
cabeca->chave = -1;
cabeca->prox = NULL;
return cabeca;
}
TNo *alocaNo(int k)
{
TNo *novo = NULL;
novo = (TNo *)malloc(sizeof(TNo));
if(novo) //if(novo!=NULL)
{
novo->chave = k;
novo->prox = NULL;
}
else
{
printf("\nMemória não alocada");
return NULL;
}
return novo;
}
void insereLista2(TNo *lista, int k) {
TNo *novo = alocaNo(k);
if (novo == NULL) {
return;
}
novo->prox = lista->prox;
lista->prox = novo;
}
void imprimeLista(TNo *p)
{
//assert(p);
if(p) //p!=NULL
{
while(p!=NULL)
{
printf("%d\n", p->chave);
p = p->prox;
}
}
else
printf("Lista vazia\n");
}
void desalocaNo2(TNo *lista) {
TNo *aux = lista->prox;
if(aux == NULL) {
printf("Lista vazia\n");
return;
}
lista->prox = aux->prox;
free(aux);
aux = NULL;
}