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++;
}
public E pop() throws Exception{
E result=null;
if (this.isEmpty()){
throw new Exception("La cola está vacía");
}
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;
}
public E peek() throws Exception{
E result=null;
if (this.isEmpty()){
throw new Exception("La Cola está vacía");
}
result = this.data[0];
return result;
}
public E peekLast() throws Exception{
E result=null;
if (this.isEmpty()){
throw new Exception("La Cola está vacía");
}
result = this.data[size-1];
return result;
}
@Override
public String toString()
{
String result = " ";
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");
System.out.println(myQueue);
try {
myQueue.pop();
} catch (Exception ex) {
Logger.getLogger(ClientQueue.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(myQueue);
}
}
Primer resultado en google :¬¬
https://www.geeksforgeeks.org/reversing-a-queue/
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");
System.out.println(myQueue);
try {
myQueue.pop();
} catch (Exception ex) {
Logger.getLogger(ClientQueue.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(myQueue);
System.out.println("cola invertida: ");
myQueue.invert();
System.out.println(myQueue);
}
}
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++;
}
public E pop() throws Exception{
E result=null;
if (this.isEmpty()){
throw new Exception("La cola está vacía");
}
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;
}
public E peek() throws Exception{
E result=null;
if (this.isEmpty()){
throw new Exception("La Cola está vacía");
}
result = this.data[0];
return result;
}
public E peekLast() throws Exception{
E result=null;
if (this.isEmpty()){
throw new Exception("La Cola está vacía");
}
result = this.data[size-1];
return result;
}
@Override
public String toString()
{
String result = " ";
for (int i= size-1; i >= 0; i--)
{
result += this.data[i] + " " ;
}
return result;
}
public void invert(){
Stack aux = new Stack();
Queue result = new Queue();
while(!result.isEmpty()){
try {
aux.push(result.peek());
result.pop();
} catch (Exception ex) {
Logger.getLogger(Queue.class.getName()).log(Level.SEVERE, null, ex);
}
}
while(!aux.isEmpty()){
try {
result.push(aux.peek());
aux.pop();
} catch (Exception ex) {
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;
public Stack() {
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++;
}
public E pop() throws Exception{
E result = null;
if (this.isEmpty()){
throw new Exception("La Pila está vacía");
}
this.size--;
result = this.data[this.size];
this.data[this.size]= null;
return result;
}
public E peek() throws Exception{
E result = null;
if (this.isEmpty()){
throw new Exception("La Pila está vacía");
}
result = this.data[this.size];
return result;
}
}
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) {
Queue<String> myQueue = new LinkedList();
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/
Cita de: EdePC en 14 Febrero 2019, 00:53 AM
-- 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<>();
Cita de: 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
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.