-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedstack.h
More file actions
108 lines (98 loc) · 2.42 KB
/
Copy pathlinkedstack.h
File metadata and controls
108 lines (98 loc) · 2.42 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
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
struct no {
int val;
struct no* prox;
};
struct linkedstack {
struct no* topo;
int qtdade;
};
struct linkedstack* inicializar() {
struct linkedstack* pilha = (struct linkedstack*)malloc(sizeof(struct linkedstack));
pilha->topo = 0;
pilha->qtdade = 0;
return pilha;
}
struct no* alocarNovoNo(int valor) {
struct no* novoNo = (struct no*)malloc(sizeof(struct no));
novoNo->val = valor;
novoNo->prox = 0;
return novoNo;
}
//retornar true se a pilha for nula ou vazia
bool vazia(struct linkedstack* pilha) {
if (pilha == NULL || pilha->qtdade == 0) {
return true;
}
else {
return false;
}
}
//se a pilha estiver nula, instancie a pilha/
void empilhar(struct linkedstack** pilha, int valor) {
if (*pilha == NULL) {
*pilha = inicializar();
}
if ((*pilha)->topo == 0) {
(*pilha)->topo = alocarNovoNo(valor);
(*pilha)->qtdade++;
}
else {
struct no* inicio;
inicio = (*pilha)->topo;
(*pilha)->topo = alocarNovoNo(valor);
(*pilha)->topo->prox = inicio;
(*pilha)->qtdade++;
}
}
//decrementar qtdade se a pilha não estiver nula ou vazia
void desempilhar(struct linkedstack* pilha) {
if (pilha == NULL) {
pilha = inicializar();
}
struct no* aux = pilha->topo;
if (pilha->qtdade > 0) {
pilha->topo = pilha->topo->prox;
free(aux);
pilha->qtdade--;
}
}
//retorne a constante INT_MIN se a pilha for nula ou vazia
int desempilharRetornando(struct linkedstack* pilha) {
if (pilha == NULL || pilha->qtdade == 0) {
return INT_MIN;
}
else {
int aux;
aux = pilha->topo->val;
desempilhar(pilha);
return aux;
}
}
//retorne a constante INT_MIN se a pilha for nula ou vazia
int topo(struct linkedstack* pilha) {
if (pilha == NULL || pilha->qtdade == 0) {
return INT_MIN;
}
else {
return pilha->topo->val;
}
}
void exibirPilha(struct linkedstack* pilha) {
//usamos o aux para percorrer a lista
if (!vazia(pilha)) {
struct no* aux = pilha->topo;
//navega partindo do topo até chegar NULL
printf("_\n");
do {
printf("%d\n", aux->val);
aux = aux->prox;
} while (aux != NULL);
printf("_");
}
else {
printf("A pilha está vazia!");
}
}