From 2f4c70ad4c114e2b02185d8643477e3f7687ec4b Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 25 Jun 2016 14:14:17 -0400 Subject: [PATCH] Lista enlazada added. --- .../cromer/estructuras/ArrayController.java | 2 + src/cl/cromer/estructuras/ColaController.java | 1 + src/cl/cromer/estructuras/Grafico.java | 42 +++++ src/cl/cromer/estructuras/ListaEnlazada.java | 38 +++++ .../estructuras/ListaEnlazdaController.java | 155 ++++++++++-------- src/cl/cromer/estructuras/PilaController.java | 1 + .../estructuras/bundles/Idioma_en.properties | 6 + .../estructuras/bundles/Idioma_es.properties | 6 + .../estructuras/fxml/listaEnlazada.fxml | 5 +- 9 files changed, 186 insertions(+), 70 deletions(-) diff --git a/src/cl/cromer/estructuras/ArrayController.java b/src/cl/cromer/estructuras/ArrayController.java index d844462..3aef7b6 100644 --- a/src/cl/cromer/estructuras/ArrayController.java +++ b/src/cl/cromer/estructuras/ArrayController.java @@ -134,6 +134,7 @@ public class ArrayController implements Initializable { try { boolean exito = array.insertar(Integer.valueOf(valorArray.getText())); if (exito) { + valorArray.setText(""); generarGrafico(); } else { @@ -179,6 +180,7 @@ public class ArrayController implements Initializable { if (valorArray.getText() != null && !valorArray.getText().trim().equals("")) { boolean exito = array.eliminar(Integer.valueOf(valorArray.getText())); if (exito) { + valorArray.setText(""); generarGrafico(); } else { diff --git a/src/cl/cromer/estructuras/ColaController.java b/src/cl/cromer/estructuras/ColaController.java index 36df888..27d6dfa 100644 --- a/src/cl/cromer/estructuras/ColaController.java +++ b/src/cl/cromer/estructuras/ColaController.java @@ -129,6 +129,7 @@ public class ColaController implements Initializable { try { if (cola.size() < 10) { cola.push(Integer.valueOf(valorCola.getText())); + valorCola.setText(""); generarGrafico(); } else { diff --git a/src/cl/cromer/estructuras/Grafico.java b/src/cl/cromer/estructuras/Grafico.java index 75af501..393ff34 100644 --- a/src/cl/cromer/estructuras/Grafico.java +++ b/src/cl/cromer/estructuras/Grafico.java @@ -7,6 +7,7 @@ import javafx.scene.Scene; import javafx.scene.layout.StackPane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; +import javafx.scene.shape.Line; import javafx.scene.shape.Rectangle; import javafx.scene.text.Text; import javafx.util.Duration; @@ -65,6 +66,22 @@ public class Grafico { destacado = -1; } + /** + * Crear una linea vertical + * @return StackPane: Devolver el stackpane que contiene la linea vertical. + */ + public static StackPane crearLineaVertical() { + Line line = new Line(); + line.setStartX(20); + line.setEndX(20); + line.setStartY(0); + line.setEndY(20); + + StackPane stackPane = new StackPane(); + stackPane.getChildren().addAll(line); + return stackPane; + } + /** * Crear un rectangulo con texto adentro. * @param colores Colores: Los colores para dar color al rectangulo. @@ -111,6 +128,31 @@ public class Grafico { return stackPane; } + /** + * Crear un rectangulo con texto adentro. + * @param colores Colores: Los colores para dar color al rectangulo. + * @param label String: El texto por el ID de fxml. + * @param texto String: El texto a colocar dentro el rectangulo. + * @param tamano int: El tamaño del rectangulo. + * @return StackPane: Devolver el stackpane que contiene el rectangulo y texto. + */ + public static StackPane crearCaja(Colores colores, String label, String texto, int tamano) { + Rectangle rectangle = new Rectangle(); + rectangle.setHeight(tamano); + rectangle.setWidth(tamano); + rectangle.setFill(colores.getFondo()); + rectangle.setStroke(Color.BLACK); + rectangle.setId("border_" + label); + Text text = new Text(); + text.setId("caja_" + label); + text.setStroke(colores.getTexto()); + text.setText(texto); + + StackPane stackPane = new StackPane(); + stackPane.getChildren().addAll(rectangle, text); + return stackPane; + } + /** * Crear un animacion de transicion usando colores que cambian. * @param rectangle Rectangle: El objeto que desea animar. diff --git a/src/cl/cromer/estructuras/ListaEnlazada.java b/src/cl/cromer/estructuras/ListaEnlazada.java index db6577e..e32e8f6 100644 --- a/src/cl/cromer/estructuras/ListaEnlazada.java +++ b/src/cl/cromer/estructuras/ListaEnlazada.java @@ -3,10 +3,26 @@ package cl.cromer.estructuras; public class ListaEnlazada { private Enlace lista; + private int size; + + private int tipo; + public ListaEnlazada() { lista = null; } + public int size() { + return size; + } + + public int getTipo() { + return tipo; + } + + public void setTipo(int tipo) { + this.tipo = tipo; + } + public Enlace buscar(int llave) { if (this.lista != null) { // La lista no es vacia @@ -39,6 +55,7 @@ public class ListaEnlazada { nueva.setValor(valor); nueva.setSiguente(lista); lista = nueva; + size++; return true; } else { @@ -73,6 +90,7 @@ public class ListaEnlazada { // Sino cortar esta enlace de la lista previo.setSiguente(lista.getSiguente()); } + size--; return true; } else { @@ -81,6 +99,26 @@ public class ListaEnlazada { } } + /** + * Devolver un enlace con su llave y valor. + * @param indice int: El indice que desea ver. + * @return Enlace: El enlace a devolver. + */ + public Enlace getIndice(int indice) { + if (lista != null && indice >= 0 && indice < size()) { + int i = size(); + Enlace lista = this.lista; + while (i > indice + 1) { + lista = lista.getSiguente(); + i--; + } + return lista; + } + else { + return null; + } + } + // Estructura de enlaces public class Enlace { private int llave; diff --git a/src/cl/cromer/estructuras/ListaEnlazdaController.java b/src/cl/cromer/estructuras/ListaEnlazdaController.java index ac043ee..678beb8 100644 --- a/src/cl/cromer/estructuras/ListaEnlazdaController.java +++ b/src/cl/cromer/estructuras/ListaEnlazdaController.java @@ -6,6 +6,7 @@ import javafx.scene.Scene; import javafx.scene.control.ButtonBar; import javafx.scene.control.ButtonType; import javafx.scene.control.Dialog; +import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.text.Text; @@ -20,14 +21,18 @@ import java.util.logging.Level; * @author Chris Cromer */ public class ListaEnlazdaController implements Initializable { + /** + * La caja para ingresar la llave. + */ + @FXML private TextFieldLimited llaveLista; /** - * La caja para ingresar textos. + * La caja para ingresar el valor. */ @FXML private TextFieldLimited valorLista; /** - * Donde poner el contenido de array. + * Donde poner el contenido de lista. */ @FXML private VBox contenidoLista; @@ -47,15 +52,25 @@ public class ListaEnlazdaController implements Initializable { private ResourceBundle resourceBundle; /** - * El array usado en la aplicación. + * La lista enlazada usado en la aplicación. */ - private Array array; + private ListaEnlazada listaEnlazada; /** - * Grafico rectangulos. + * Tipo de lista enlazada a trabajar. + */ + private ListaEnlazadaTipos listaEnlazadaTipos; + + /** + * Grafico rectangulos y lineas. */ private Grafico grafico; + /** + * Colores por los dibjos. + */ + private Colores colores; + /** * Inicializar todos los datos y dibujar las graficas. * @param location URL: El URL de fxml en uso. @@ -66,21 +81,15 @@ public class ListaEnlazdaController implements Initializable { this.resourceBundle = resourceBundle; scene = null; - Colores colores = new Colores(); - - for (int i = 0; i < 10; i++) { - contenidoLista.getChildren().addAll(Grafico.crearCaja(colores, String.valueOf(i))); - colores.siguinteColor(); - } } /** - * Llenar el array con numeros al azar. + * Llenar la lista con numeros al azar. */ @FXML protected void botonLlenar() { if (scene == null) { - initializeArray(); + initializeLista(); } Random random = new Random(); @@ -88,66 +97,70 @@ public class ListaEnlazdaController implements Initializable { int minimo = 0; int rango = maximo - minimo + 1; - for (int i = array.size(); i < 10; i++) { + for (int i = listaEnlazada.size(); listaEnlazada.size() < 5; i++) { int numero = random.nextInt(rango) + minimo; - while (array.buscar(numero) != -1) { - numero = random.nextInt(rango) + minimo; + while (listaEnlazada.buscar(i) != null) { + i++; } - array.insertar(numero); + listaEnlazada.insertar(i, numero); } generarGrafico(); } /** - * Vaciar el array de todos los valores. + * Vaciar la lista de todos los valores. */ @FXML protected void botonVaciar() { if (scene == null) { - initializeArray(); + initializeLista(); } - if (array.isOrdered()) { - array = new Array(10); - array.setOrdered(true); - } - else { - array = new Array(10); - array.setOrdered(false); - } + listaEnlazada = new ListaEnlazada(); generarGrafico(); } /** - * Insertar un valor al array y mostrar el codigo en la pantalla. + * Insertar un valor a la lista y mostrar el codigo en la pantalla. */ @FXML protected void botonInsertar() { if (scene == null) { - initializeArray(); + initializeLista(); + } + + String tipo; + switch (listaEnlazadaTipos.getTipo()) { + case ListaEnlazadaTipos.SIMPLE: + tipo = "Simple"; + break; + case ListaEnlazadaTipos.CIRCULAR: + tipo = "Circular"; + break; + case ListaEnlazadaTipos.DOBLEMENTE_ENLAZADA: + tipo = "Doblemente"; + break; + default: + tipo = "Simple"; } // Mostrar el codigo - String tipo = (array.isOrdered())?"Ordenado":"Simple"; - String codigoTexto = new Scanner(getClass().getResourceAsStream("/cl/cromer/estructuras/code/array" + tipo + "/insertar")).useDelimiter("\\Z").next(); - codigoLista.setText(codigoTexto); + //String codigoTexto = new Scanner(getClass().getResourceAsStream("/cl/cromer/estructuras/code/listaEnlazada" + tipo + "/insertar")).useDelimiter("\\Z").next(); + //codigoLista.setText(codigoTexto); - if (valorLista.getText() != null && !valorLista.getText().trim().equals("")) { + if (llaveLista.getText() != null && !llaveLista.getText().trim().equals("") && valorLista.getText() != null && !valorLista.getText().trim().equals("")) { try { - boolean exito = array.insertar(Integer.valueOf(valorLista.getText())); + boolean exito = listaEnlazada.insertar(Integer.valueOf(llaveLista.getText()), Integer.valueOf(valorLista.getText())); if (exito) { + llaveLista.setText(""); + valorLista.setText(""); generarGrafico(); } else { ButtonType botonCerrar = new ButtonType(resourceBundle.getString("cerrar"), ButtonBar.ButtonData.OK_DONE); Dialog dialog = new Dialog<>(); dialog.setTitle(resourceBundle.getString("error")); - if (array.size() == 10) { - dialog.setContentText(resourceBundle.getString("arrayLleno")); - } - else { - dialog.setContentText(resourceBundle.getString("arrayValorExiste")); - } + dialog.setContentText(resourceBundle.getString("listaLlaveExiste")); dialog.getDialogPane().getButtonTypes().add(botonCerrar); dialog.show(); } @@ -164,23 +177,25 @@ public class ListaEnlazdaController implements Initializable { } /** - * Eliminar un valor del array si existe y mostrar el codigo en la pantalla. + * Eliminar un valor de la lista si existe y mostrar el codigo en la pantalla. */ @FXML protected void botonEliminar() { if (scene == null) { - initializeArray(); + initializeLista(); } // Mostrar el codigo - String tipo = (array.isOrdered())?"Ordenado":"Simple"; - String codigoTexto = new Scanner(getClass().getResourceAsStream("/cl/cromer/estructuras/code/array" + tipo + "/eliminar")).useDelimiter("\\Z").next(); - codigoLista.setText(codigoTexto); + //String tipo = (array.isOrdered())?"Ordenado":"Simple"; + //String codigoTexto = new Scanner(getClass().getResourceAsStream("/cl/cromer/estructuras/code/array" + tipo + "/eliminar")).useDelimiter("\\Z").next(); + //codigoLista.setText(codigoTexto); try { - if (valorLista.getText() != null && !valorLista.getText().trim().equals("")) { - boolean exito = array.eliminar(Integer.valueOf(valorLista.getText())); + if (llaveLista.getText() != null && !llaveLista.getText().trim().equals("")) { + boolean exito = listaEnlazada.eliminar(Integer.valueOf(llaveLista.getText())); if (exito) { + llaveLista.setText(""); + valorLista.setText(""); generarGrafico(); } else { @@ -199,27 +214,27 @@ public class ListaEnlazdaController implements Initializable { } /** - * Buscar si existe un elemento en el array y mostrar el codigo en la pantalla - * Si existe el valor destacarlo. + * Buscar si existe una llave en la lista y mostrar el codigo en la pantalla + * Si existe la llave destacarla. */ @FXML protected void botonBuscar() { if (scene == null) { - initializeArray(); + initializeLista(); } // Mostrar el codigo - String tipo = (array.isOrdered())?"Ordenado":"Simple"; - String codigoTexto = new Scanner(getClass().getResourceAsStream("/cl/cromer/estructuras/code/array" + tipo + "/buscar")).useDelimiter("\\Z").next(); - codigoLista.setText(codigoTexto); + //String tipo = (array.isOrdered())?"Ordenado":"Simple"; + //String codigoTexto = new Scanner(getClass().getResourceAsStream("/cl/cromer/estructuras/code/array" + tipo + "/buscar")).useDelimiter("\\Z").next(); + //codigoLista.setText(codigoTexto); try { - if (valorLista.getText() != null && !valorLista.getText().trim().equals("")) { - int encontrado = array.buscar(Integer.valueOf(valorLista.getText())); - if (encontrado != -1) { + if (llaveLista.getText() != null && !llaveLista.getText().trim().equals("")) { + ListaEnlazada.Enlace enlace = listaEnlazada.buscar(Integer.valueOf(llaveLista.getText())); + if (enlace != null) { generarGrafico(); grafico = new Grafico(scene); - grafico.destacer(encontrado, Grafico.RECTANGULO); + grafico.destacer(enlace.getLlave(), Grafico.RECTANGULO); } else { errorNoEsta(); @@ -237,38 +252,37 @@ public class ListaEnlazdaController implements Initializable { } /** - * Se muestra un error si la persona no ingresa un valor en el TextField. + * Se muestra un error si la persona no ingresa un valor y una llave en los TextField. */ private void errorNoValor() { ButtonType botonCerrar = new ButtonType(resourceBundle.getString("cerrar"), ButtonBar.ButtonData.OK_DONE); Dialog dialog = new Dialog<>(); dialog.setTitle(resourceBundle.getString("error")); - dialog.setContentText(resourceBundle.getString("arrayNoValor")); + dialog.setContentText(resourceBundle.getString("listaNoValor")); dialog.getDialogPane().getButtonTypes().add(botonCerrar); dialog.show(); } /** - * Error cuando el valor no está en el array. + * Error cuando la llave no está en la lista. */ private void errorNoEsta() { ButtonType botonCerrar = new ButtonType(resourceBundle.getString("cerrar"), ButtonBar.ButtonData.OK_DONE); Dialog dialog = new Dialog<>(); dialog.setTitle(resourceBundle.getString("error")); - dialog.setContentText(resourceBundle.getString("arrayNoEsta")); + dialog.setContentText(resourceBundle.getString("listaNoEsta")); dialog.getDialogPane().getButtonTypes().add(botonCerrar); dialog.show(); } /** - * Crear el array de tamaño 10. La scene está usado para saber si es de tipo ordenado o simple segun el ménu. + * Crear una lista vacia. */ - private void initializeArray() { + private void initializeLista() { scene = contenidoLista.getScene(); grafico = new Grafico(scene); - this.array = new Array(10); - Array array = (Array) scene.getUserData(); - this.array.setOrdered(array.isOrdered()); + this.listaEnlazada = new ListaEnlazada(); + listaEnlazadaTipos = (ListaEnlazadaTipos) scene.getUserData(); } /** @@ -276,9 +290,12 @@ public class ListaEnlazdaController implements Initializable { */ private void generarGrafico() { grafico.removerDestacar(); - for (int i = 0; i < 10; i++) { - Text text = (Text) scene.lookup("#caja_" + String.valueOf(i)); - text.setText(array.getIndice(i)); + colores = new Colores(); + contenidoLista.getChildren().clear(); + for (int i = 0; i < listaEnlazada.size(); i++) { + ListaEnlazada.Enlace enlace = listaEnlazada.getIndice(i); + contenidoLista.getChildren().addAll(Grafico.crearCaja(colores, String.valueOf(enlace.getLlave()), String.valueOf(enlace.getLlave()) + " | " + String.valueOf(enlace.getValor()), 50), Grafico.crearLineaVertical()); + colores.siguinteColor(); } } } diff --git a/src/cl/cromer/estructuras/PilaController.java b/src/cl/cromer/estructuras/PilaController.java index 4a944a5..0a0a5ef 100644 --- a/src/cl/cromer/estructuras/PilaController.java +++ b/src/cl/cromer/estructuras/PilaController.java @@ -129,6 +129,7 @@ public class PilaController implements Initializable { try { if (pila.size() < 10) { pila.push(Integer.valueOf(valorPila.getText())); + valorPila.setText(""); generarGrafico(); } else { diff --git a/src/cl/cromer/estructuras/bundles/Idioma_en.properties b/src/cl/cromer/estructuras/bundles/Idioma_en.properties index af6a38d..013df98 100644 --- a/src/cl/cromer/estructuras/bundles/Idioma_en.properties +++ b/src/cl/cromer/estructuras/bundles/Idioma_en.properties @@ -56,6 +56,8 @@ cancelar=Cancel cerrar=Close error=Error +llave=Key: +valor=Value: llenar=Fill vaciar=Empty insertar=Insert @@ -85,6 +87,10 @@ quickYaOrdenado=The array is already sorted. mergeYaOrdenado=The array is already sorted. +listaLlaveExiste=Key already exists. +listaNoEsta=Key does not exist. +listaNoValor=Please input a numeric key and value. + pilaLlena=Value not inserted because the stack is full. pilaVacia=The stack is empty. pilaNoValor=Please input a numeric value. diff --git a/src/cl/cromer/estructuras/bundles/Idioma_es.properties b/src/cl/cromer/estructuras/bundles/Idioma_es.properties index 2e30387..859e3cb 100644 --- a/src/cl/cromer/estructuras/bundles/Idioma_es.properties +++ b/src/cl/cromer/estructuras/bundles/Idioma_es.properties @@ -56,6 +56,8 @@ cancelar=Cancelar cerrar=Cerrar error=Error +llave=Llave: +valor=Valor: vaciar=Vaciar llenar=Llenar insertar=Insertar @@ -85,6 +87,10 @@ quickYaOrdenado=El array ya est\u00E1 ordenado. mergeYaOrdenado=El array ya est\u00E1 ordenado. +listaLlaveExiste=La llave ya existe. +listaNoEsta=La llave no existe. +listaNoValor=Ingresar una llave y valor num\u00E9ricos por favor. + pilaLlena=Valor no fue insertado porque la pila est\u00E1 llena. pilaVacia=La pila est\u00E1 vac\u00EDa. pilaNoValor=Ingresar un valor num\u00E9rico por favor. diff --git a/src/cl/cromer/estructuras/fxml/listaEnlazada.fxml b/src/cl/cromer/estructuras/fxml/listaEnlazada.fxml index 9ba595b..0bb67ca 100644 --- a/src/cl/cromer/estructuras/fxml/listaEnlazada.fxml +++ b/src/cl/cromer/estructuras/fxml/listaEnlazada.fxml @@ -18,7 +18,10 @@