Problems with bicola in java, I want to insert data from both sides in a queue, but it does not work correctly

0

This is the problem I'm doing a bicola to insert data in both sides of the queue, but when I insert data to the left it does not work for me, instead putting data on the right side if it works.

public class NodoCola {
    public int dato;
    public NodoCola siguiente;

    public int getDato() {
        return dato;
    }

    public void setDato(int dato) {
        this.dato = dato;
    }

    public NodoCola getSiguiente() {
        return siguiente;
    }

    public void setSiguiente(NodoCola siguiente) {
        this.siguiente = siguiente;
    }

    public NodoCola(int dato){
        this.dato=dato;
        this.siguiente= null;

    }


// --------------------------Clase Cola--------------------------
public class Cola {

    NodoCola inicio, fin;
    int tama;

    public Cola() {
        inicio = fin = null;
        tama = 0;
    }

    public boolean colaVacia() {
        return inicio == null;
    }
    public void insertarDatoDerecha(int elem){
        NodoCola nuevo= new NodoCola(elem);
        if (colaVacia()) {
            nuevo.setDato(elem);
            inicio = nuevo;
        } else {
            nuevo.setDato(elem);
            fin.siguiente = nuevo;
        }
        fin = nuevo;
        tama++;
    }

    public void insertarDatoIzquierda(int elem) {
        NodoCola nuevo = new NodoCola(elem);
        if (colaVacia()) {
            nuevo.setDato(elem);
            fin= nuevo;

        } else{
            nuevo.setDato(elem);
            fin=nuevo.siguiente;
    }
        inicio=nuevo;
        tama++;
    }
    
asked by Oswaldo 31.10.2017 в 23:49
source

1 answer

1

First of all you have a wrong concept of tail or bicola (based on what you implemented), a bicola is a collection of objects one after another in which you can insert or delete the first or the last of your data.

Now with respect to what you implemented what you have is a tree since you have your object NodeCola Next inside your NodeCola So instead of having one element after another, you have one element inside another.

Once this is cleared, I will omit the fact that we are calling a tree as a queue and I will help you solve the problem of your code which is the main reason why the question was opened.

First you must know that if you pass parameters through the constructor

NodoCola nuevo= new NodoCola(elem);

You do not need to pass the same parameter again through the set method

nuevo.setDato(elem);

Second in your tail class you have 2 NodeCola

defined
NodoCola inicio, fin;

You must bear in mind that you are working with only one structure and not with two, regardless of whether this structure has a start element and an end element (rather an element that contains all and an element that is contained in it). all) So you only have to work with a NodeCola object

Third never handle global variables is your classes of logic (business), is considered a bad practice, because your code is not scalable and is difficult to decouple. As an example, imagine that later the user wants to insert in 2 different queues but inserting the inserts between amabas as your queue variable is global and in it your inserts are saved you must make more modifications so that this code can work, when the user Handles 2 or more queues. For this reason I advise you to receive as a parameter the queue with which to work and return it, if you do this when the user wants to manage 2 or more queues the only thing you send is the queue in which you want to insert without having to modify the business logic.

public class Cola {

 public static void main (String[] args) {
   NodoCola cola = new NodoCola();
   Cola obj = new Cola();

   cola = obj.insertarDatoInterior(cola,3);
   cola = obj.insertarDatoInterior(cola,4);
   cola = obj.insertarDatoExterior(cola,2);
   cola = obj.insertarDatoInterior(cola,5);
   cola = obj.insertarDatoExterior(cola,1);
   cola = obj.insertarDatoInterior(cola,6);
   cola = obj.insertarDatoExterior(cola,0);
   obj.imprimeCola(cola);

 }
 public NodoCola insertarDatoInterior(NodoCola cola, int elem) {
   NodoCola elementoCola = cola;

   //Si su dato es null significa que la cola es nueva
   if(elementoCola.getDato() == null) {
     elementoCola.setDato(elem);
   }

   //La cola ya existe
   else { 
     //Busca el ultimo elemento anidado
     while (elementoCola.getSiguiente() != null){
       elementoCola = elementoCola.getSiguiente();
     } 
     //Crea nuevo elemento que se guardara en el utimo elemento anidado
     NodoCola nuevoElemento = new NodoCola();
     nuevoElemento.setDato(elem);

     //Guarda el nuevoElemento en el ultimo elemento anidado
     elementoCola.setSiguiente(nuevoElemento);
   }

   return cola;
 }

 public NodoCola insertarDatoExterior(NodoCola cola, int elem) {

   //Si su dato es null significa que la cola es nueva
   if(cola.getDato() == null) {
     cola.setDato(elem);
     return cola;
   }

   //Si ya existe
   else {
     //Crea el nuevo elemento que contendra la cola ya existente
     NodoCola nuevoElemento = new NodoCola();
     nuevoElemento.setDato(elem);

     //Se anida la cola existente dentro del nuevo elemento
     nuevoElemento.setSiguiente(cola);
     return nuevoElemento;
   }

 }
 public void imprimeCola(NodoCola cola) {
   int i=0;
   NodoCola elemento = cola;
   while(elemento.getDato() != null){
     System.out.println("Elemento "+ i +" es "+elemento.getDato());
     if(elemento.getSiguiente() != null) {
       elemento = elemento.getSiguiente();
     }
     else {
       break;
     }
     i++;
   }
 }
}

Note : even though I included the main in the same class it was just because of laziness, you do not put the main there, in your class NodoCola change the data type data to Integer since with it you can handle the data as null before the user adds something.

    
answered by 01.11.2017 в 20:15