Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Pilha {
- private int[] pilha;
- private int capacidade;
- private int topo;
- public Pilha(int capacidade) {
- this.capacidade = capacidade;
- this.topo = -1;
- this.pilha = new int[capacidade];
- }
- public boolean isEmpty() {
- return this.topo == -1;
- }
- public boolean isFull() {
- return this.topo == (capacidade - 1);
- }
- public void push(int n) {
- if (this.isFull())
- throw new RuntimeException("FullStackException");
- this.topo += 1;
- this.pilha[topo] = n;
- }
- public int pop() {
- if (this.isEmpty())
- throw new RuntimeException("EmptyStackException");
- int valor_topo = this.pilha[topo];
- topo -= 1;
- return valor_topo;
- }
- public int peek() {
- if (this.isEmpty())
- throw new RuntimeException("EmptyStackException");
- return this.pilha[topo];
- }
- public int size() {
- return this.topo + 1;
- }
- public static void main(String[] args) {
- Pilha pilha = new Pilha(5);
- assert pilha.isEmpty();
- assert !pilha.isFull();
- assert pilha.size() == 0;
- pilha.push(10);
- assert pilha.peek() == 10;
- assert pilha.size() == 1;
- assert !pilha.isEmpty();
- pilha.push(5);
- assert pilha.size() == 2;
- assert pilha.pop() == 5;
- pilha.push(7);
- assert pilha.peek() == 7;
- pilha.pop();
- assert !pilha.isEmpty();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement