forked from matheusfer0902/Simulador-ED
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPilha.cpp
79 lines (65 loc) · 1.26 KB
/
Pilha.cpp
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
/*
* Pilha.cpp
*
* Created on: 20 de out. de 2022
* Author: tiagomaritan
*/
#include "Pilha.h"
using namespace std;
Pilha::Pilha() {
topo = -1;
}
Pilha::~Pilha() {
}
/** Verifica se a Pilha está vazia */
bool Pilha::vazia(){
if (topo == -1)
return true;
else
return false;
}
/**Verifica se a Pilha está cheia */
bool Pilha::cheia(){
if (topo == (TAM_MAX -1))
return true;
else
return false;
}
/**Obtém o tamanho da Pilha*/
int Pilha::tamanho(){
return topo+1;
}
/** Consulta o elemento do topo da Pilha.
Retorna -1 se a pilha estiver vazia,
caso contrário retorna o valor que está no topo da pilha. */
int Pilha::top() {
if (vazia())
return -1; // pilha vazia
return dados[topo];
}
/** Insere um elemento no topo da pilha.
Retorna false se a pilha estiver cheia.
Caso contrário retorna true */
bool Pilha::push(int valor) {
if (cheia())
return false; // err: pilha cheia
topo++;
dados[topo] = valor;
return true;
}
/** Retira o elemento do topo da pilha.
Retorna -1 se a pilha estiver vazia. */
int Pilha::pop() {
if (vazia())
return -1; // Pilha vazia
int valor = dados[topo];
topo--;
return valor;
}
int Pilha::sequencia(int ordem)
{
if(ordem <= topo && ordem >= 0)
return dados[ordem];
else
return -1;
}