-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
96 lines (69 loc) · 2.26 KB
/
Copy pathStack.java
File metadata and controls
96 lines (69 loc) · 2.26 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
public class Stack<T> {
// * Atributos
private T[] elements;
private int size;
// * Construtores
public Stack(int capacity) {
this.elements = (T[]) new Object[capacity];
this.size = 0;
}
public Stack() {
this(10);
}
// * Métodos de Controle Interno
//Aumenta a capacidade do array interno dessa instância List, se necessário, dinamicamente
private void ensureCapacity() {
if (this.size == this.elements.length) {
T[] newElements = (T[]) new Object[this.size * 2];
for (int i = 0; i < this.size; i++) newElements[i] = this.elements[i];
this.elements = newElements;
}
}
// * Métodos de Inserção
//Empurra um item para o topo desta pilha.
public T push(T element) {
this.ensureCapacity();
if (this.size < this.elements.length) {
this.elements[this.size] = element;
this.size++;
}
return element;
}
// * Métodos de Consulta
//Retorna o número de elementos nesta lista.
public int size() {
return this.size;
}
//Retorna verdadeiro se esta lista não contiver elementos.
public boolean isEmpty() {
return this.size == 0;
}
//Olha para o objeto no topo desta pilha sem removê-lo da pilha.
public T peek() {
if (this.isEmpty()) return null;
return this.elements[this.size - 1];
}
//Retorna uma representação string do objeto
public String toString() {
StringBuilder stack = new StringBuilder();
stack.append("[");
for (int i = 0; i < this.size - 1; i++) {
stack.append(this.elements[i]);
stack.append(", ");
}
if (this.size > 0) stack.append(this.elements[this.size - 1]);
stack.append("]");
return stack.toString();
}
// * Métodos de Remoção
//Remove o objeto no topo desta pilha e retorna esse objeto como o valor desta função.
public T pop() {
if (this.isEmpty()) return null;
return this.elements[--this.size];
}
//Remove todos os elementos desta lista.
public void clear() {
for (int i = 0; i < this.size; i++) this.elements[i] = null;
this.size = 0;
}
}