fefetl08

Pilha_Encadeada

Apr 8th, 2020
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. public class Pilha {
  2.     private ListaEncadeadaDupla pilha;
  3.  
  4.     public Pilha() {
  5.         this.pilha = new ListaEncadeadaDupla();
  6.     }
  7.  
  8.     public boolean vazia() {
  9.         return pilha.vazia();
  10.     }
  11.  
  12.     public void empilha(int dado) {
  13.         pilha.insereUltimo(dado);
  14.     }
  15.  
  16.     public Node desempilha() {
  17.         Node inicio = pilha.getLista();
  18.         Node aux = inicio;
  19.         while (aux.getProximo() != inicio)
  20.             aux = aux.getProximo();
  21.         pilha.removeUltimo();
  22.         return aux;
  23.     }
  24.  
  25.     public Integer topo(){
  26.         Node inicio = pilha.getLista();
  27.         Node aux = inicio;
  28.         if(vazia()) {
  29.             return null;
  30.         }
  31.         else if (aux.getProximo() == inicio) {
  32.             return aux.getInformacao();
  33.         }
  34.         while (aux.getProximo() != inicio) {
  35.             aux = aux.getProximo();
  36.         }
  37.         return aux.getInformacao();
  38.     }
  39.  
  40.     public void mostrar_pilha() {
  41.         pilha.imprime();
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment