Título: invertir cola
Publicado por: sheiking en 13 Febrero 2019, 17:23 pm
hola que tal cree una cola con los métodos básicos de agregar eliminar y mostrar los elementos de la cola pero me gustaría saber como podría invertir los datos de este eh buscado en varios post acerca de como invertir una cola pero no eh podido solucionar mi problema me gustaría saber si me pueden ayudar aquí les adjunto el código package clientqueue; public class Queue<E> { public static final int CAPACITY =100000; private E[] data; private int size=0; public Queue() { this. data = (E [])new Object[this. CAPACITY]; } public boolean isEmpty(){ return (this.size==0); } public int size(){ return (this.size); } public void push(E value){ this.data[this.size] = value; this.size++; } E result=null; if (this.isEmpty()){ } result = this.data[0]; for (int i=0;i<this.size-1;i++){ data[i]=data[i+1]; } this.data[this.size]= null; this.size--; return result; } E result=null; if (this.isEmpty()){ } result = this.data[0]; return result; } E result=null; if (this.isEmpty()){ } result = this.data[size-1]; return result; } @Override { for (int i= size-1; i >= 0; i--) { result += this.data[i] + " " ; } return result; } }
package clientqueue; import java.util.logging.Level; import java.util.logging.Logger; public class ClientQueue { public static void main (String[] args ) { Queue<String> myQueue = new Queue(); myQueue.push("Jesus"); myQueue.push("Alberto"); myQueue.push("Enrique"); myQueue.push("Isma"); myQueue.push("Alexis"); try { myQueue.pop(); Logger.getLogger(ClientQueue.class.getName()).log(Level.SEVERE, null, ex); } } }
Título: Re: invertir cola
Publicado por: ThunderCls en 13 Febrero 2019, 18:35 pm
Primer resultado en google :¬¬
https://www.geeksforgeeks.org/reversing-a-queue/
Título: Re: invertir cola
Publicado por: sheiking en 13 Febrero 2019, 19:35 pm
amigo probe lo que me dices cree una clase llamada stack con sus metodos pero me sigue saliendo los mismos datos iguales aqui esta el codigo package clientqueue; import java.util.logging.Level; import java.util.logging.Logger; public class ClientQueue { public static void main (String[] args ) { Queue<String> myQueue = new Queue(); myQueue.push("Jesus"); myQueue.push("Alberto"); myQueue.push("Enrique"); myQueue.push("Isma"); myQueue.push("Alexis"); try { myQueue.pop(); Logger.getLogger(ClientQueue.class.getName()).log(Level.SEVERE, null, ex); } System. out. println("cola invertida: "); myQueue.invert(); } }
package clientqueue; import java.util.logging.Level; import java.util.logging.Logger; public class Queue<E> { public static final int CAPACITY =100000; private E[] data; private int size=0; public Queue() { this. data = (E [])new Object[this. CAPACITY]; } public boolean isEmpty(){ return (this.size==0); } public int size(){ return (this.size); } public void push(E value){ this.data[this.size] = value; this.size++; } E result=null; if (this.isEmpty()){ } result = this.data[0]; for (int i=0;i<this.size-1;i++){ data[i]=data[i+1]; } this.data[this.size]= null; this.size--; return result; } E result=null; if (this.isEmpty()){ } result = this.data[0]; return result; } E result=null; if (this.isEmpty()){ } result = this.data[size-1]; return result; } @Override { for (int i= size-1; i >= 0; i--) { result += this.data[i] + " " ; } return result; } public void invert(){ Queue result = new Queue(); while(!result.isEmpty()){ try { aux.push(result.peek()); result.pop(); Logger.getLogger(Queue.class.getName()).log(Level.SEVERE, null, ex); } } while(!aux.isEmpty()){ try { result.push(aux.peek()); aux.pop(); Logger.getLogger(Queue.class.getName()).log(Level.SEVERE, null, ex); } } } }
package clientqueue; public class Stack<E> { public static final int CAPACITY =100000; private E[] data; private int size=0; this. data = (E [])new Object[this. CAPACITY]; } public boolean isEmpty(){ return (this.size==0); } public int size(){ return (this.size); } public void push(E value){ this.data[this.size] = value; this.size++; } E result = null; if (this.isEmpty()){ } this.size--; result = this.data[this.size]; this.data[this.size]= null; return result; } E result = null; if (this.isEmpty()){ } result = this.data[this.size]; return result; } }
Título: Re: invertir cola
Publicado por: EdePC en 14 Febrero 2019, 00:53 am
Saludos, - Tamaño código :xD, con esto me funciona a mí, intenta injertarlo en tu código: import java.util.*; class ReverseQueue { public static void main (String[] args ) { myQueue.add( "Jesus" ); myQueue.add( "Alberto" ); myQueue.add( "Enrique" ); myQueue.add( "Isma" ); myQueue.add( "Alexis" ); System. out. println( "\nCola tal cual" ); for ( String str : myQueue ) { System. out. println( "\t" + str ); } // Reverse a la cola Stack <String > stack = new Stack(); while ( !myQueue.isEmpty() ) { stack.add( myQueue.peek() ); myQueue.remove(); } while ( !stack.isEmpty() ) { myQueue.add( stack.peek() ); stack.pop(); } System. out. println( "\nCola al revez" ); for ( String str : myQueue ) { System. out. println( "\t" + str ); } } }
c:\Users\EdSon\Desktop>javac ReverseQueue.java && java ReverseQueue Note: ReverseQueue.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.
Cola tal cual Jesus Alberto Enrique Isma Alexis
Cola al revez Alexis Isma Enrique Alberto Jesus
c:\Users\EdSon\Desktop> -- Mmm..., me pone nervioso ese Unsafed Operations >:D - Referencias: https://www.geeksforgeeks.org/reversing-a-queue/
Título: Re: invertir cola
Publicado por: CalgaryCorpus en 23 Febrero 2019, 13:27 pm
-- Mmm..., me pone nervioso ese Unsafed Operations >:D
Tienes que definir la linkedList y el Stack usando el diamond operator: Queue<String> myQueue = new LinkedList<>(); ... Stack<String> stack = new Stack<>();
Título: Re: invertir cola
Publicado por: CalgaryCorpus en 23 Febrero 2019, 13:38 pm
amigo probe lo que me dices cree una clase llamada stack con sus metodos pero me sigue saliendo los mismos datos iguales
Creo que lo que explica que tu metodo invert() no funcione es esta: 1. El metodo invert que definiste crea una Queue de nombre result, que por supuesto originalmente esta vacia. 2. El primer ciclo no hace nada pues la cola esta vacia, por lo que el stack definido para la ocasion no se toca en absoluto. 3. El segundo ciclo no hace nada pues el stack esta vacio, por lo dicho anteriormente, y 4. luego no se hace nada con la cola result.
|