-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTAD.c
More file actions
71 lines (59 loc) · 1.51 KB
/
Copy pathTAD.c
File metadata and controls
71 lines (59 loc) · 1.51 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
#include <stdio.h>
#include <stdlib.h>
#include "TAD.h"
//================== PILHA ===========================
void inicializarPilha(PILHA *p){
p->topo = NULL;
}
void liberarPilha(PILHA *p) {
while (p->topo != NULL) {
Coordenada *temp = p->topo;
p->topo = p->topo->proximo;
free(temp);
}
}
void push(PILHA *p, int x, int y, int ultimoMovimento){
Coordenada *ptr = (Coordenada*) malloc(sizeof(Coordenada));
if (ptr == NULL) {
printf("Erro: falha na alocação de memória!\n");
exit(1);
}
ptr->x = x;
ptr->y = y;
ptr->ultimaJogada = ultimoMovimento;
ptr->proximo = p->topo;
p->topo = ptr;
}
int *pop(PILHA *p){
int ultimoMovimento;
int *valores = (int*) malloc(3*sizeof(int));
if (p->topo == NULL) {
free(valores);
return NULL;
}
Coordenada *ptr = p->topo;
valores[0] = ptr->ultimaJogada;
valores[1] = ptr->x;
valores[2] = ptr->y;
p->topo = ptr->proximo;
free(ptr);
return valores;
}
void imprimirPilha(PILHA *p){
Coordenada *ptr = p->topo;
Coordenada *aux;
PILHA *auxiliar = (PILHA *) malloc(sizeof(PILHA));;
inicializarPilha (auxiliar);
while(ptr != NULL){
push(auxiliar, ptr->x, ptr->y, 1);
ptr = ptr->proximo;
}
aux = auxiliar->topo;
while(aux != NULL){
printf("(%d,%d)", aux->x, aux->y);
aux = aux->proximo;
}
liberarPilha(auxiliar);
free(auxiliar);
}
//==================================================