Ir ao conteúdo
  • Cadastre-se

Inerter pilha em java


Posts recomendados

Caros estou com dificuldades em fazer um construtor que inverta uma pilha, a assinatura do método tem que ser:

 public Stack reverse(), preciso colocar esse método na seguinte classe Starck :

package estruturas_sequenciais;

 

public class Stack {
    private Object s[];
    private int top = -1;

    public Stack(int size) {
        s = new Object;
    }
   
    public boolean isEmpty() {
        if (top == -1)
            return true;
        return false;
    }
 
    public boolean isFull() {
        if (top == s.length - 1)
            return true;
        return false;
    }
 
    public void push(Object obj) throws OverflowException {
        if (!isFull()) {
            s[++top] = obj;
        } else
            throw new OverflowException();
    }
 
    public Object pop() throws UnderflowException {
        if (!isEmpty()) {
            Object o = s[top];
            s[top] = null;
            top--;
            return o;
        } else
            throw new UnderflowException();
 
    }
   
    public void imprime()
    {
      for(int i = ; i <= top; i ++)
      System.out.print(s);
   
    }
 
    public Stack reverse()
    throws UnderflowException, OverflowException
    {
     
        if (!isEmpty()) {
      
        }
        return X;
    }   
 
}

 

Link para o comentário
Compartilhar em outros sites

public class Stack {
    private Object s[];
    private int top = -1;

    public Stack(int size) { //construtor que recebe como parametro o tamanho
        s = new Object;
    }
   
    public boolean isEmpty() {  //verifica se a pilha está vazia
        if (top == -1)
            return true;
        return false;
    }
 
    public boolean isFull() {  // verifica se a pilha está cheia
        if (top == s.length - 1)
            return true;
        return false;
    }
 
    public void push(Object obj) throws OverflowException { // insere um objeto na pilha via parametro
        if (!isFull()) {
            s[++top] = obj;
        } else
            throw new OverflowException();
    }
 
    public Object pop() throws UnderflowException {  // retira um objeto da pilha
        if (!isEmpty()) {
            Object o = s[top];
            s[top] = null;
            top--;
            return o;
        } else
            throw new UnderflowException();
 
    }
   
    public void imprime() // imprime o conteudo da pilha
    {
      for(int i = ; i <= top; i ++)
      System.out.print(s);
   
    }
 
    public Stack reverse()         // inverte o conteudo da pilha  ( Falta)
    throws UnderflowException, OverflowException
    {
     
       
    }   
 
}

 

Link para o comentário
Compartilhar em outros sites

 public Stack reverse()
    throws UnderflowException, OverflowException
    {
        Stack result = new Stack(s.length);
        Stack aux = new Stack(s.length);

        for(int i = 0; i <= top; i ++)
            aux.push(s[i]);
        
        while(!aux.isEmpty()){
            for(int i = 0; i <= top; i ++)
                result.push(aux.pop());
        }

        return result;
    }

Consegui ! :lol:

Link para o comentário
Compartilhar em outros sites

Visitante
Este tópico está impedido de receber novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...