Hello! I am pretty new to java, so pleasee be patient with me !!
Ok, so I am trying to remove a value from a stack that is not in the top of the stack using only stacks, so I basically created 2 stacks pila and pilaTemp and move values from one another and then push back the values a move to the second stack, not sure if I explain my self, but anyways here is the code. I have a problem with the loops I think, but I've been trying to understand what I am doing wrong but nothing comes to my mind, and what I try does not work. For example.. I am able to remove the top value (32).. but if I want to remove 3, it won't do anything! If anyone can send me in the right direction I'll appreciate it! thanks!!
Here is the code:
public class Pila { Stack<Integer> pila = new Stack<>(); Stack<Integer> pilaTemp = new Stack<>(); //Stack<Integer> pilaDestino = new Stack<>(); public void Pila() { pila.push(4); pila.push(20); pila.push(25); pila.push(3); pila.push(32); } public void sacaNumero(int n){ for(int i = 0; i<pila.size(); i++){ if(pila.peek()== n){ pila.pop(); } else if(pila.peek()!= n){ do{ pilaTemp.push(pila.pop()); if(pila.peek() == n) pila.pop(); pila.push(pilaTemp.pop()); } while(pila.peek() != n && pila != null); } else if(pila.isEmpty() || pila.search(n) == -1){ System.out.print("No se puede completar la acción. El número no existe o la pila está vacía"); } System.out.print(pila); } }