Skip to content

Instantly share code, notes, and snippets.

@klenne
Last active November 22, 2018 11:29
Show Gist options
  • Save klenne/73f40d7b5507dc5402a1d6aac5d10ffd to your computer and use it in GitHub Desktop.
Save klenne/73f40d7b5507dc5402a1d6aac5d10ffd to your computer and use it in GitHub Desktop.
package pilha;
import java.util.Scanner;
/**
* @author Klenne
*
*/
public class Pilha {
int vet[] = new int[100];
int topo = -1;
Scanner s = new Scanner(System.in);
public void mostra() {
if (topo < 0) {
System.out.print("A pilha está vazia\n");
} else {
System.out.println("\n Mostrando a pilha:\n");
for (int i = topo; i >= 0; i--) {
System.out.println(vet[i]);
}
}
}
public void empilha() {
System.out.println("Digite o número para empilhar:");
topo++;
vet[topo] = s.nextInt();
mostra();
}
public void desempilha() {
if (topo <0) {
System.out.print("A pilha está vazia\n");
} else {
topo--;
mostra();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment