-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathPilha.java
67 lines (57 loc) · 1.53 KB
/
Pilha.java
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
package pilha;
public class Pilha {
public Object[] pilha;
public int posicaoPilha;
public Pilha() {
this.posicaoPilha = -1;
// indica que esta nula, vazia, pois posição um indica que contém informação
this.pilha = new Object[1000];
// criando uma pilha com 1000 posições
}
public boolean pilhaVazia() {
//isEmpty
if (this.posicaoPilha == -1) {
return true;
}
return false;
}
public int tamanho() {
//is
if (this.pilhaVazia()) {
return 0;
}
return this.posicaoPilha + 1;
}
public Object exibeUltimoValor() {
//top
if (this.pilhaVazia()) {
return null;
}
return this.pilha[this.posicaoPilha];
}
public Object desempilhar() {
//pop
if (pilhaVazia()) {
return null;
}
return this.pilha[this.posicaoPilha--];
}
public void empilhar(Object valor) {
// push
if (this.posicaoPilha < this.pilha.length - 1) {
this.pilha[++posicaoPilha] = valor;
}
}
public static void main(String args[]) {
Pilha p = new Pilha();
p.empilhar("Portuguesa ");
p.empilhar("Frango com catupiry ");
p.empilhar("Calabresa ");
p.empilhar("Quatro queijos ");
p.empilhar(10);
//Listar a pilha
while (p.pilhaVazia() == false) {
System.out.println(p.desempilhar());
}
}
}