From 49f0064d49e67824ab702f8bfdef7ced45a93918 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Sat, 2 Jul 2016 18:04:18 -0400 Subject: [PATCH] Cleanup code. --- src/cl/cromer/estructuras/Array.java | 909 +++++++++--------- .../cromer/estructuras/ArrayController.java | 62 +- .../cromer/estructuras/BurbujaController.java | 69 +- src/cl/cromer/estructuras/Cola.java | 84 +- src/cl/cromer/estructuras/ColaController.java | 33 +- src/cl/cromer/estructuras/HashTable.java | 35 +- .../estructuras/InsercionController.java | 66 +- .../estructuras/ListaEnlazdaController.java | 253 +++-- src/cl/cromer/estructuras/MenuController.java | 156 ++- .../cromer/estructuras/MergeController.java | 66 +- src/cl/cromer/estructuras/Pila.java | 84 +- src/cl/cromer/estructuras/PilaController.java | 33 +- .../cromer/estructuras/QuickController.java | 66 +- .../estructuras/SeleccionController.java | 70 +- .../cromer/estructuras/ShellController.java | 72 +- 15 files changed, 975 insertions(+), 1083 deletions(-) diff --git a/src/cl/cromer/estructuras/Array.java b/src/cl/cromer/estructuras/Array.java index d15f95d..29da2c6 100644 --- a/src/cl/cromer/estructuras/Array.java +++ b/src/cl/cromer/estructuras/Array.java @@ -1,487 +1,530 @@ package cl.cromer.estructuras; +import java.util.Random; + /** * Crear una estructura de dato de tipo array. * * @author Chris Cromer */ final public class Array { - /** - * El array. - */ - final private String array[]; + /** + * El array. + */ + final private String array[]; - /** - * La cantidad de elementos en el array. - */ - private int size; + /** + * La cantidad de elementos en el array. + */ + private int size; - /** - * Si es de tipo ordenado o simple. - */ - private boolean ordered; + /** + * Si es de tipo ordenado o simple. + */ + private boolean ordered; - /** - * Crear el array con el tamaño pasador por argumento. - * - * @param tamano int: El tamaño del array a crear. - */ - public Array(int tamano) { - this.array = new String[tamano]; - size = 0; - ordered = false; - } + /** + * Crear el array con el tamaño pasador por argumento. + * + * @param tamano int: El tamaño del array a crear. + */ + public Array(int tamano) { + this.array = new String[tamano]; + size = 0; + ordered = false; + } - /** - * Devolver la cantidad de elementos en el array. - * - * @return int: Devolver la cantidad de elementos en el array. - */ - public int size() { - return size; - } + /** + * Dovolver si el tipo es ordenado o no. + * + * @return boolean: Si el tipo de array es ordenado. + */ + public boolean isOrdered() { + return ordered; + } - /** - * Dovolver si el tipo es ordenado o no. - * - * @return boolean: Si el tipo de array es ordenado. - */ - public boolean isOrdered() { - return ordered; - } + /** + * Cambiar el tipo de array entre ordenado o simple. + * + * @param ordered boolean: Si es verdad, es de tipo ordenado, sino el tipo es simple. + */ + public void setOrdered(boolean ordered) { + this.ordered = ordered; + } - /** - * Cambiar el tipo de array entre ordenado o simple. - * - * @param ordered boolean: Si es verdad, es de tipo ordenado, sino el tipo es simple. - */ - public void setOrdered(boolean ordered) { - this.ordered = ordered; - } + /** + * Eliminar un valor del array si existe. + * + * @param valor int: El valor a eliminar. + * + * @return boolean: Verdad si fue encontrado y borrado, sino falso. + */ + public boolean eliminar(int valor) { + boolean borrado = false; + for (int i = 0; i < array.length; i++) { + if (array[i] != null && array[i].equals(String.valueOf(valor))) { + // Eliminar el valor + array[i] = null; + borrado = true; + size--; + if (ordered) { + for (int j = i; j < array.length; j++) { + if (j != array.length - 1) { + // Correr la array hacia arriba + array[j] = array[j + 1]; + } + } + array[array.length - 1] = null; + } + else { + break; + } + } + } + return borrado; + } - /** - * Insertar un valor al array. - * - * @param valor int: El valor a insertar. - * @return boolean: Verdad si fue exitoso, sino falso. - */ - public boolean insertar(int valor) { - for (int i = 0; i < array.length; i++) { - if (array[i] == null) { - array[i] = String.valueOf(valor); - size++; - return true; - } - else if (array[i].equals(String.valueOf(valor))) { - // Ya existe el valor en el array - return false; - } - } - return false; - } + /** + * Devolver el valor que está guardado en cada indice del array. Se usa para construir la grafica. + * + * @param indice int: El indice que desea ver. + * + * @return String: El valor que está en dicho indice. + */ + public String getIndice(int indice) { + if (indice >= 0 && indice < array.length) { + return array[indice]; + } + else { + return null; + } + } - /** - * Eliminar un valor del array si existe. - * - * @param valor int: El valor a eliminar. - * @return boolean: Verdad si fue encontrado y borrado, sino falso. - */ - public boolean eliminar(int valor) { - boolean borrado = false; - for (int i = 0; i < array.length; i++) { - if (array[i] != null && array[i].equals(String.valueOf(valor))) { - // Eliminar el valor - array[i] = null; - borrado = true; - size--; - if (ordered) { - for (int j = i; j < array.length; j++) { - if (j != array.length - 1) { - // Correr la array hacia arriba - array[j] = array[j + 1]; - } - } - array[array.length - 1] = null; - } - else { - break; - } - } - } - return borrado; - } + /** + * Llenar el array con valores al azar. + */ + public void llenar() { + Random random = new Random(); + int maximo = 99; + int minimo = 0; + int rango = maximo - minimo + 1; - /** - * Buscar si existe un valor dentro el array. - * - * @param valor int: Valor a buscar. - * @return int: Devuelve el indice donde fue encontrado, o -1 si no fue encontrado. - */ - public int buscar(int valor) { - for (int i = 0; i < array.length; i++) { - if (array[i] != null && array[i].equals(String.valueOf(valor))) { - // Se encontró - return i; - } - } - // No se encontró - return -1; - } + for (int i = size(); i < 10; i++) { + int numero = random.nextInt(rango) + minimo; + while (buscar(numero) != - 1) { + numero = random.nextInt(rango) + minimo; + } + insertar(numero); + } + } - /** - * Devolver el valor que está guardado en cada indice del array. Se usa para construir la grafica. - * - * @param indice int: El indice que desea ver. - * @return String: El valor que está en dicho indice. - */ - public String getIndice(int indice) { - if (indice >= 0 && indice < array.length) { - return array[indice]; - } - else { - return null; - } - } + /** + * Devolver la cantidad de elementos en el array. + * + * @return int: Devolver la cantidad de elementos en el array. + */ + public int size() { + return size; + } - /** - * Ordenar el array usando burbuja. - * - * @param paso boolean: Si es verdad, solo hago en paso del ordenamiento, sino ordenear todos los elementos. - * @return boolean: Verdad si algo cambió, sino falso. - */ - public boolean burbuja(boolean paso) { - boolean cambio = false; + /** + * Buscar si existe un valor dentro el array. + * + * @param valor int: Valor a buscar. + * + * @return int: Devuelve el indice donde fue encontrado, o -1 si no fue encontrado. + */ + public int buscar(int valor) { + for (int i = 0; i < array.length; i++) { + if (array[i] != null && array[i].equals(String.valueOf(valor))) { + // Se encontró + return i; + } + } + // No se encontró + return - 1; + } - for (int i = size() - 1; i > 1; i--) { - for (int j = 0; j < i; j++) { - if (Integer.valueOf(array[j]) > Integer.valueOf(array[j + 1])) { - String temp = array[j]; - array[j] = array[j + 1]; - array[j + 1] = temp; - cambio = true; - if (paso) { - return true; - } - } - } - } + /** + * Insertar un valor al array. + * + * @param valor int: El valor a insertar. + * + * @return boolean: Verdad si fue exitoso, sino falso. + */ + public boolean insertar(int valor) { + for (int i = 0; i < array.length; i++) { + if (array[i] == null) { + array[i] = String.valueOf(valor); + size++; + return true; + } + else if (array[i].equals(String.valueOf(valor))) { + // Ya existe el valor en el array + return false; + } + } + return false; + } - return cambio; - } + /** + * Borrar el array. + */ + public void nuevo() { + for (int i = 0; i < size(); i++) { + array[i] = null; + } + } - /** - * Ordenar el array usando inserción. - * - * @param paso boolean: Si es verdad, solo hago en paso del ordenamiento, sino ordenear todos los elementos. - * @return boolean: Verdad si algo cambió, sino falso. - */ - public boolean insercion(boolean paso) { - boolean cambio = false; + /** + * Ordenar el array usando burbuja. + * + * @param paso boolean: Si es verdad, solo hago en paso del ordenamiento, sino ordenear todos los elementos. + * + * @return boolean: Verdad si algo cambió, sino falso. + */ + public boolean burbuja(boolean paso) { + boolean cambio = false; - for (int i = 1; i < size(); i++) { - String temp = array[i]; - int j = i; - while (j > 0 && Integer.valueOf(array[j - 1]) >= Integer.valueOf(temp)) { - array[j] = array[j - 1]; - --j; - cambio = true; - } - array[j] = temp; - if (paso && cambio) { - return true; - } - } + for (int i = size() - 1; i > 1; i--) { + for (int j = 0; j < i; j++) { + if (Integer.valueOf(array[j]) > Integer.valueOf(array[j + 1])) { + String temp = array[j]; + array[j] = array[j + 1]; + array[j + 1] = temp; + cambio = true; + if (paso) { + return true; + } + } + } + } - return cambio; - } + return cambio; + } - /** - * Ordenar el array usando selección. - * - * @param paso boolean: Si es verdad, solo hago en paso del ordenamiento, sino ordenear todos los elementos. - * @return boolean: Verdad si algo cambió, sino falso. - */ - public boolean seleccion(boolean paso) { - boolean cambio = false; + /** + * Ordenar el array usando inserción. + * + * @param paso boolean: Si es verdad, solo hago en paso del ordenamiento, sino ordenear todos los elementos. + * + * @return boolean: Verdad si algo cambió, sino falso. + */ + public boolean insercion(boolean paso) { + boolean cambio = false; - for (int i = 0; i < size() - 1; i++) { - int minimo = i; - for (int j = i + 1; j < size(); j++) { - if (Integer.valueOf(array[j]) < Integer.valueOf(array[minimo])) { - minimo = j; - cambio = true; - } - } - String temp = array[i]; - array[i] = array[minimo]; - array[minimo] = temp; - if (paso && cambio) { - return true; - } - } + for (int i = 1; i < size(); i++) { + String temp = array[i]; + int j = i; + while (j > 0 && Integer.valueOf(array[j - 1]) >= Integer.valueOf(temp)) { + array[j] = array[j - 1]; + -- j; + cambio = true; + } + array[j] = temp; + if (paso && cambio) { + return true; + } + } - return cambio; - } + return cambio; + } - /** - * Ordenar el array usando shell. - * - * @param paso boolean: Si es verdad, solo hago en paso del ordenamiento, sino ordenear todos los elementos. - * @return boolean: Verdad si algo cambió, sino falso. - */ - public boolean shell(boolean paso) { - boolean cambio = false; + /** + * Ordenar el array usando selección. + * + * @param paso boolean: Si es verdad, solo hago en paso del ordenamiento, sino ordenear todos los elementos. + * + * @return boolean: Verdad si algo cambió, sino falso. + */ + public boolean seleccion(boolean paso) { + boolean cambio = false; - int j, i; - String temp; + for (int i = 0; i < size() - 1; i++) { + int minimo = i; + for (int j = i + 1; j < size(); j++) { + if (Integer.valueOf(array[j]) < Integer.valueOf(array[minimo])) { + minimo = j; + cambio = true; + } + } + String temp = array[i]; + array[i] = array[minimo]; + array[minimo] = temp; + if (paso && cambio) { + return true; + } + } - int h = 1; - while (h <= size() / 3) { - h = h * 3 + 1; - } + return cambio; + } - while (h > 0) { - for (i = h; i < size(); i++) { - temp = array[i]; - j = i; - while (j > h - 1 && Integer.valueOf(array[j - h]) >= Integer.valueOf(temp)) { - array[j] = array[j - h]; - j -= h; - cambio = true; - } - array[j] = temp; - if (paso && cambio) { - return true; - } - } - h = (h - 1) / 3; - } + /** + * Ordenar el array usando shell. + * + * @param paso boolean: Si es verdad, solo hago en paso del ordenamiento, sino ordenear todos los elementos. + * + * @return boolean: Verdad si algo cambió, sino falso. + */ + public boolean shell(boolean paso) { + boolean cambio = false; - return cambio; - } + int j, i; + String temp; - /** - * Ordenar el array usando quick. - * - * @param paso boolean: Si es verdad, solo hago en paso del ordenamiento, sino ordenear todos los elementos. - * @return boolean: Verdad si algo cambió, sino falso. - */ - public boolean quick(boolean paso) { - String array[] = this.array.clone(); - boolean cambio = false; - boolean cambio2; - cambio2 = recurenciaQuick(0, size() - 1, paso); - for (int i = 0; i < size(); i++) { - if (!array[i].equals(this.array[i])) { - cambio = true; - } - } - return (cambio || cambio2); - } + int h = 1; + while (h <= size() / 3) { + h = h * 3 + 1; + } - /** - * Metodo recursivo para ordenar using quick sort. - * - * @param izquerda int: La posición del quick desded la izquerda. - * @param derecha int: La posición del quick desded la derecha.. - * @param paso boolean: Si es verdad, solo hago en paso del ordenamiento, sino ordenear todos los elementos. - * @return boolean: Verdad si algo cambió, sino falso. - */ - private boolean recurenciaQuick(int izquerda, int derecha, boolean paso) { - boolean cambio; - boolean cambio2; + while (h > 0) { + for (i = h; i < size(); i++) { + temp = array[i]; + j = i; + while (j > h - 1 && Integer.valueOf(array[j - h]) >= Integer.valueOf(temp)) { + array[j] = array[j - h]; + j -= h; + cambio = true; + } + array[j] = temp; + if (paso && cambio) { + return true; + } + } + h = (h - 1) / 3; + } - if (derecha - izquerda <= 0) { - return false; - } - else { - String pivot = array[derecha]; + return cambio; + } - ParticionarResult particionarResult = particionar(izquerda, derecha, pivot); - if (paso && particionarResult.getCambio()) { - return particionarResult.getCambio(); - } - cambio = recurenciaQuick(izquerda, particionarResult.getPunteroIzquerda() - 1, paso); - cambio2 = recurenciaQuick(particionarResult.getPunteroIzquerda() + 1, derecha, paso); - return (paso && (cambio || cambio2)); - } - } + /** + * Ordenar el array usando quick. + * + * @param paso boolean: Si es verdad, solo hago en paso del ordenamiento, sino ordenear todos los elementos. + * + * @return boolean: Verdad si algo cambió, sino falso. + */ + public boolean quick(boolean paso) { + String array[] = this.array.clone(); + boolean cambio = false; + boolean cambio2; + cambio2 = recurenciaQuick(0, size() - 1, paso); + for (int i = 0; i < size(); i++) { + if (! array[i].equals(this.array[i])) { + cambio = true; + } + } + return (cambio || cambio2); + } - /** - * Particionar el array desded la izquerda y derecho usando un pivot. - * - * @param izquerda int: La posición del quick desde la izquerda. - * @param derecha int: La posición del quick desde la derecha. - * @param pivot String: El valor a comparar con los otros. - * @return ParticionarResult: Los resultados de particionar. - */ - private ParticionarResult particionar(int izquerda, int derecha, String pivot) { - boolean cambio = false; + /** + * Metodo recursivo para ordenar using quick sort. + * + * @param izquerda int: La posición del quick desded la izquerda. + * @param derecha int: La posición del quick desded la derecha.. + * @param paso boolean: Si es verdad, solo hago en paso del ordenamiento, sino ordenear todos los elementos. + * + * @return boolean: Verdad si algo cambió, sino falso. + */ + private boolean recurenciaQuick(int izquerda, int derecha, boolean paso) { + boolean cambio; + boolean cambio2; - int punteroIzquerda = izquerda - 1; - int punteroDerecha = derecha; - while (true) { - //noinspection StatementWithEmptyBody - while (Integer.valueOf(array[++punteroIzquerda]) < Integer.valueOf(pivot)) { - } - //noinspection StatementWithEmptyBody - while (punteroDerecha > 0 && Integer.valueOf(array[--punteroDerecha]) > Integer.valueOf(pivot)) { - } + if (derecha - izquerda <= 0) { + return false; + } + else { + String pivot = array[derecha]; - if (punteroIzquerda >= punteroDerecha) { - break; - } - else { - String temp = array[punteroIzquerda]; - array[punteroIzquerda] = array[punteroDerecha]; - array[punteroDerecha] = temp; - cambio = true; - } - } - String temp = array[punteroIzquerda]; - array[punteroIzquerda] = array[derecha]; - array[derecha] = temp; + ParticionarResult particionarResult = particionar(izquerda, derecha, pivot); + if (paso && particionarResult.getCambio()) { + return particionarResult.getCambio(); + } + cambio = recurenciaQuick(izquerda, particionarResult.getPunteroIzquerda() - 1, paso); + cambio2 = recurenciaQuick(particionarResult.getPunteroIzquerda() + 1, derecha, paso); + return (paso && (cambio || cambio2)); + } + } - return new ParticionarResult(cambio, punteroIzquerda); - } + /** + * Particionar el array desded la izquerda y derecho usando un pivot. + * + * @param izquerda int: La posición del quick desde la izquerda. + * @param derecha int: La posición del quick desde la derecha. + * @param pivot String: El valor a comparar con los otros. + * + * @return ParticionarResult: Los resultados de particionar. + */ + private ParticionarResult particionar(int izquerda, int derecha, String pivot) { + boolean cambio = false; - /** - * Ordenar el array usando merge. - * - * @param paso boolean: Si es verdad, solo hago en paso del ordenamiento, sino ordenear todos los elementos. - * @return boolean: Verdad si algo cambió, sino falso. - */ - public boolean merge(boolean paso) { - String array[] = this.array.clone(); - boolean cambio = false; - boolean cambio2; - String[] temp = new String[size()]; - cambio2 = recurenciaMerge(temp, 0, size() - 1, paso); - for (int i = 0; i < size(); i++) { - if (!array[i].equals(this.array[i])) { - cambio = true; - } - } - return (cambio || cambio2); - } + int punteroIzquerda = izquerda - 1; + int punteroDerecha = derecha; + while (true) { + //noinspection StatementWithEmptyBody + while (Integer.valueOf(array[++ punteroIzquerda]) < Integer.valueOf(pivot)) { + } + //noinspection StatementWithEmptyBody + while (punteroDerecha > 0 && Integer.valueOf(array[-- punteroDerecha]) > Integer.valueOf(pivot)) { + } - /** - * El metodo recursivo para ordenar con merge. - * - * @param temp String[]: El array temporario para trabajar. - * @param izquerda int: El lado izquerda. - * @param derecha int: El lado derecha. - * @param paso boolean: Verdad si es paso por paso. - * @return boolean: Devolver si algo cambió. - */ - private boolean recurenciaMerge(String[] temp, int izquerda, int derecha, boolean paso) { - if (izquerda != derecha) { - boolean cambio; - boolean cambio2; - boolean cambio3; + if (punteroIzquerda >= punteroDerecha) { + break; + } + else { + String temp = array[punteroIzquerda]; + array[punteroIzquerda] = array[punteroDerecha]; + array[punteroDerecha] = temp; + cambio = true; + } + } + String temp = array[punteroIzquerda]; + array[punteroIzquerda] = array[derecha]; + array[derecha] = temp; - int medio = (izquerda + derecha) / 2; - cambio = recurenciaMerge(temp, izquerda, medio, paso); - if (paso && cambio) { - return true; - } - cambio2 = recurenciaMerge(temp, medio + 1, derecha, paso); - if (paso && cambio2) { - return true; - } - cambio3 = merge(temp, izquerda, medio + 1, derecha, paso); - return (paso && cambio3); - } - else { - return false; - } - } + return new ParticionarResult(cambio, punteroIzquerda); + } - /** - * Este metodo hace los cambios al array. - * - * @param temp String[]: El array temporario para trabajar. - * @param prevIzquerda int: El valor previo de la izquerda. - * @param prevMedio int: El valor previo al medio. - * @param prevDerecha int: El valor previo de la derecha. - * @param paso boolean: Si es paso por paso. - * @return boolean: Devolver si algo cambió. - */ - private boolean merge(String[] temp, int prevIzquerda, int prevMedio, int prevDerecha, boolean paso) { - boolean cambio = false; - int j = 0; - int izquerda = prevIzquerda; - int medio = prevMedio - 1; - int derecha = prevDerecha - izquerda + 1; + /** + * Ordenar el array usando merge. + * + * @param paso boolean: Si es verdad, solo hago en paso del ordenamiento, sino ordenear todos los elementos. + * + * @return boolean: Verdad si algo cambió, sino falso. + */ + public boolean merge(boolean paso) { + String array[] = this.array.clone(); + boolean cambio = false; + boolean cambio2; + String[] temp = new String[size()]; + cambio2 = recurenciaMerge(temp, 0, size() - 1, paso); + for (int i = 0; i < size(); i++) { + if (! array[i].equals(this.array[i])) { + cambio = true; + } + } + return (cambio || cambio2); + } - while (prevIzquerda <= medio && prevMedio <= prevDerecha) { - if (Integer.valueOf(array[prevIzquerda]) < Integer.valueOf(array[prevMedio])) { - temp[j++] = array[prevIzquerda++]; - } - else { - temp[j++] = array[prevMedio++]; - } - } + /** + * El metodo recursivo para ordenar con merge. + * + * @param temp String[]: El array temporario para trabajar. + * @param izquerda int: El lado izquerda. + * @param derecha int: El lado derecha. + * @param paso boolean: Verdad si es paso por paso. + * + * @return boolean: Devolver si algo cambió. + */ + private boolean recurenciaMerge(String[] temp, int izquerda, int derecha, boolean paso) { + if (izquerda != derecha) { + boolean cambio; + boolean cambio2; + boolean cambio3; - while (prevIzquerda <= medio) { - temp[j++] = array[prevIzquerda++]; - } + int medio = (izquerda + derecha) / 2; + cambio = recurenciaMerge(temp, izquerda, medio, paso); + if (paso && cambio) { + return true; + } + cambio2 = recurenciaMerge(temp, medio + 1, derecha, paso); + if (paso && cambio2) { + return true; + } + cambio3 = merge(temp, izquerda, medio + 1, derecha, paso); + return (paso && cambio3); + } + else { + return false; + } + } - while (prevMedio <= prevDerecha) { - temp[j++] = array[prevMedio++]; - } + /** + * Este metodo hace los cambios al array. + * + * @param temp String[]: El array temporario para trabajar. + * @param prevIzquerda int: El valor previo de la izquerda. + * @param prevMedio int: El valor previo al medio. + * @param prevDerecha int: El valor previo de la derecha. + * @param paso boolean: Si es paso por paso. + * + * @return boolean: Devolver si algo cambió. + */ + private boolean merge(String[] temp, int prevIzquerda, int prevMedio, int prevDerecha, boolean paso) { + boolean cambio = false; + int j = 0; + int izquerda = prevIzquerda; + int medio = prevMedio - 1; + int derecha = prevDerecha - izquerda + 1; - for (j = 0; j < derecha; j++) { - String temp2 = array[izquerda + j]; - array[izquerda + j] = temp[j]; - if (paso && !array[izquerda + j].equals(temp2)) { - cambio = true; - } - } - return cambio; - } + while (prevIzquerda <= medio && prevMedio <= prevDerecha) { + if (Integer.valueOf(array[prevIzquerda]) < Integer.valueOf(array[prevMedio])) { + temp[j++] = array[prevIzquerda++]; + } + else { + temp[j++] = array[prevMedio++]; + } + } - /** - * Esta clase contiene los resultados de Partricionar. - */ - final public class ParticionarResult { - /** - * Si habia algun cambio. - */ - final private boolean cambio; + while (prevIzquerda <= medio) { + temp[j++] = array[prevIzquerda++]; + } - /** - * La parte izquerda que cambió. - */ - final private int punteroIzquerda; + while (prevMedio <= prevDerecha) { + temp[j++] = array[prevMedio++]; + } - /** - * Inicializar. - * - * @param cambio boolean: Si habia un cambio o no. - * @param punteroIzquerda: El valor desde la izquerda donde fue un cambio. - */ - public ParticionarResult(boolean cambio, int punteroIzquerda) { - this.cambio = cambio; - this.punteroIzquerda = punteroIzquerda; - } + for (j = 0; j < derecha; j++) { + String temp2 = array[izquerda + j]; + array[izquerda + j] = temp[j]; + if (paso && ! array[izquerda + j].equals(temp2)) { + cambio = true; + } + } + return cambio; + } - /** - * Devolver el cambio. - * - * @return boolean: Devolver el valor de cambio. - */ - public boolean getCambio() { - return cambio; - } + /** + * Esta clase contiene los resultados de Partricionar. + */ + final public class ParticionarResult { + /** + * Si habia algun cambio. + */ + final private boolean cambio; - /** - * Devolver el puntero izquerda. - * - * @return int: Devolver el valor de puntero. - */ - public int getPunteroIzquerda() { - return punteroIzquerda; - } - } + /** + * La parte izquerda que cambió. + */ + final private int punteroIzquerda; + + /** + * Inicializar. + * + * @param cambio boolean: Si habia un cambio o no. + * @param punteroIzquerda: El valor desde la izquerda donde fue un cambio. + */ + public ParticionarResult(boolean cambio, int punteroIzquerda) { + this.cambio = cambio; + this.punteroIzquerda = punteroIzquerda; + } + + /** + * Devolver el cambio. + * + * @return boolean: Devolver el valor de cambio. + */ + public boolean getCambio() { + return cambio; + } + + /** + * Devolver el puntero izquerda. + * + * @return int: Devolver el valor de puntero. + */ + public int getPunteroIzquerda() { + return punteroIzquerda; + } + } } diff --git a/src/cl/cromer/estructuras/ArrayController.java b/src/cl/cromer/estructuras/ArrayController.java index 982e787..637ec3f 100644 --- a/src/cl/cromer/estructuras/ArrayController.java +++ b/src/cl/cromer/estructuras/ArrayController.java @@ -7,7 +7,6 @@ import javafx.scene.layout.VBox; import javafx.scene.text.Text; import java.net.URL; -import java.util.Random; import java.util.ResourceBundle; import java.util.Scanner; import java.util.logging.Level; @@ -85,21 +84,34 @@ public class ArrayController implements Initializable { initializeArray(); } - Random random = new Random(); - int maximo = 99; - int minimo = 0; - int rango = maximo - minimo + 1; - - for (int i = array.size(); i < 10; i++) { - int numero = random.nextInt(rango) + minimo; - while (array.buscar(numero) != -1) { - numero = random.nextInt(rango) + minimo; - } - array.insertar(numero); - } + array.llenar(); generarGrafico(); } + /** + * Crear el array de tamaño 10. La scene está usado para saber si es de tipo ordenado o simple segun el ménu. + */ + private void initializeArray() { + scene = contenidoArray.getScene(); + grafico = new Grafico(scene); + this.array = new Array(10); + ArrayTipos arrayTipos = (ArrayTipos) scene.getUserData(); + if (arrayTipos.getTipo() == ArrayTipos.ORDENADO) { + this.array.setOrdered(true); + } + } + + /** + * Poner los valores en el grafico. + */ + private void generarGrafico() { + grafico.removerDestacar(); + for (int i = 0; i < 10; i++) { + Text text = (Text) scene.lookup("#texto_" + String.valueOf(i)); + text.setText(array.getIndice(i)); + } + } + /** * Vaciar el array de todos los valores. */ @@ -235,28 +247,4 @@ public class ArrayController implements Initializable { Main.mostrarError(resourceBundle.getString("arrayNoValor"), resourceBundle); } } - - /** - * Crear el array de tamaño 10. La scene está usado para saber si es de tipo ordenado o simple segun el ménu. - */ - private void initializeArray() { - scene = contenidoArray.getScene(); - grafico = new Grafico(scene); - this.array = new Array(10); - ArrayTipos arrayTipos = (ArrayTipos) scene.getUserData(); - if (arrayTipos.getTipo() == ArrayTipos.ORDENADO) { - this.array.setOrdered(true); - } - } - - /** - * Poner los valores en el grafico. - */ - private void generarGrafico() { - grafico.removerDestacar(); - for (int i = 0; i < 10; i++) { - Text text = (Text) scene.lookup("#texto_" + String.valueOf(i)); - text.setText(array.getIndice(i)); - } - } } diff --git a/src/cl/cromer/estructuras/BurbujaController.java b/src/cl/cromer/estructuras/BurbujaController.java index 4a7eda7..e0976c8 100644 --- a/src/cl/cromer/estructuras/BurbujaController.java +++ b/src/cl/cromer/estructuras/BurbujaController.java @@ -3,14 +3,10 @@ package cl.cromer.estructuras; import javafx.fxml.FXML; import javafx.fxml.Initializable; 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.text.Text; import java.net.URL; -import java.util.Random; import java.util.ResourceBundle; import java.util.Scanner; @@ -54,6 +50,7 @@ public class BurbujaController implements Initializable { * @param resourceBundle ResourceBundle: Tiene datos de idioma. */ @Override + @SuppressWarnings("Duplicates") public void initialize(URL location, ResourceBundle resourceBundle) { this.resourceBundle = resourceBundle; @@ -61,21 +58,12 @@ public class BurbujaController implements Initializable { Colores colores = new Colores(); - Random random = new Random(); - int maximo = 99; - int minimo = 0; - int rango = maximo - minimo + 1; - array = new Array(10); array.setOrdered(true); + array.llenar(); for (int i = 0; i < 10; i++) { - int numero = random.nextInt(rango) + minimo; - while (array.buscar(numero) != -1) { - numero = random.nextInt(rango) + minimo; - } - array.insertar(numero); - contenidoBurbuja.getChildren().addAll(Grafico.crearCaja(colores, String.valueOf(i), String.valueOf(numero))); + contenidoBurbuja.getChildren().addAll(Grafico.crearCaja(colores, String.valueOf(i), String.valueOf(array.getIndice(i)))); colores.siguinteColor(); } } @@ -89,24 +77,28 @@ public class BurbujaController implements Initializable { initializeScene(); } - array = new Array(10); - array.setOrdered(true); - - Random random = new Random(); - int maximo = 99; - int minimo = 0; - int rango = maximo - minimo + 1; - - for (int i = array.size(); i < 10; i++) { - int numero = random.nextInt(rango) + minimo; - while (array.buscar(numero) != -1) { - numero = random.nextInt(rango) + minimo; - } - array.insertar(numero); - } + array.nuevo(); + array.llenar(); generarGrafico(); } + /** + * Crear el array de tamaño 10. + */ + private void initializeScene() { + scene = contenidoBurbuja.getScene(); + } + + /** + * Poner los valores en el grafico. + */ + private void generarGrafico() { + for (int i = 0; i < 10; i++) { + Text text = (Text) scene.lookup("#texto_" + String.valueOf(i)); + text.setText(array.getIndice(i)); + } + } + /** * Ordenarlo paso por paso. */ @@ -146,21 +138,4 @@ public class BurbujaController implements Initializable { generarGrafico(); } - - /** - * Crear el array de tamaño 10. - */ - private void initializeScene() { - scene = contenidoBurbuja.getScene(); - } - - /** - * Poner los valores en el grafico. - */ - private void generarGrafico() { - for (int i = 0; i < 10; i++) { - Text text = (Text) scene.lookup("#texto_" + String.valueOf(i)); - text.setText(array.getIndice(i)); - } - } } diff --git a/src/cl/cromer/estructuras/Cola.java b/src/cl/cromer/estructuras/Cola.java index 54f1e23..6123c41 100644 --- a/src/cl/cromer/estructuras/Cola.java +++ b/src/cl/cromer/estructuras/Cola.java @@ -1,5 +1,7 @@ package cl.cromer.estructuras; +import java.util.Random; + /** * Crear una estructura de dato de tipo cola. * @@ -24,39 +26,6 @@ final public class Cola { size = 0; } - /** - * Devolver la cantidad de elementos que están en la cola. - * - * @return int: La cantidad de elementos. - */ - public int size() { - return size; - } - - /** - * Push un valor en la cola encima. - * - * @param valor int: El valor a push. - */ - public void push(int valor) { - if (this.cola != null) { - String cola[] = new String[this.cola.length + 1]; - int i; - for (i = 0; i < this.cola.length; i++) { - cola[i] = this.cola[i]; - } - cola[i] = String.valueOf(valor); - this.cola = cola; - size++; - } - else { - String pila[] = new String[1]; - pila[0] = String.valueOf(valor); - this.cola = pila; - size++; - } - } - /** * Pop un valor del principio de la cola. * @@ -90,6 +59,15 @@ final public class Cola { } } + /** + * Devolver la cantidad de elementos que están en la cola. + * + * @return int: La cantidad de elementos. + */ + public int size() { + return size; + } + /** * Devolver el valor que está en un indice de la cola. * @@ -104,4 +82,44 @@ final public class Cola { return null; } } + + /** + * Llenar la cola con valores al azar. + */ + @SuppressWarnings("Duplicates") + public void llenar() { + Random random = new Random(); + int maximo = 99; + int minimo = 0; + int rango = maximo - minimo + 1; + + for (int i = size(); i < 10; i++) { + int numero = random.nextInt(rango) + minimo; + push(numero); + } + } + + /** + * Push un valor en la cola encima. + * + * @param valor int: El valor a push. + */ + public void push(int valor) { + if (this.cola != null) { + String cola[] = new String[this.cola.length + 1]; + int i; + for (i = 0; i < this.cola.length; i++) { + cola[i] = this.cola[i]; + } + cola[i] = String.valueOf(valor); + this.cola = cola; + size++; + } + else { + String pila[] = new String[1]; + pila[0] = String.valueOf(valor); + this.cola = pila; + size++; + } + } } diff --git a/src/cl/cromer/estructuras/ColaController.java b/src/cl/cromer/estructuras/ColaController.java index 9d04f5e..64e5db4 100644 --- a/src/cl/cromer/estructuras/ColaController.java +++ b/src/cl/cromer/estructuras/ColaController.java @@ -7,7 +7,6 @@ import javafx.scene.layout.VBox; import javafx.scene.text.Text; import java.net.URL; -import java.util.Random; import java.util.ResourceBundle; import java.util.Scanner; import java.util.logging.Level; @@ -87,18 +86,21 @@ public class ColaController implements Initializable { grafico = new Grafico(scene); } - Random random = new Random(); - int maximo = 99; - int minimo = 0; - int rango = maximo - minimo + 1; - - for (int i = cola.size(); i < 10; i++) { - int numero = random.nextInt(rango) + minimo; - cola.push(numero); - } + cola.llenar(); generarGrafico(); } + /** + * Poner los valores en el grafico. + */ + private void generarGrafico() { + grafico.removerDestacar(); + for (int i = 0; i < 10; i++) { + Text text = (Text) scene.lookup("#texto_" + String.valueOf(i)); + text.setText(cola.getIndice(i)); + } + } + /** * Vaciar la cola de todos los valores. */ @@ -201,15 +203,4 @@ public class ColaController implements Initializable { Main.mostrarError(resourceBundle.getString("colaVacia"), resourceBundle); } } - - /** - * Poner los valores en el grafico. - */ - private void generarGrafico() { - grafico.removerDestacar(); - for (int i = 0; i < 10; i++) { - Text text = (Text) scene.lookup("#texto_" + String.valueOf(i)); - text.setText(cola.getIndice(i)); - } - } } \ No newline at end of file diff --git a/src/cl/cromer/estructuras/HashTable.java b/src/cl/cromer/estructuras/HashTable.java index ddeb7a5..5c271ed 100644 --- a/src/cl/cromer/estructuras/HashTable.java +++ b/src/cl/cromer/estructuras/HashTable.java @@ -19,6 +19,7 @@ public class HashTable { * @param string String: El string a hashear. * @return int: El hash a devolver. */ + @SuppressWarnings("unused") public int hashMejor(String string) { int intLength = string.length() / 4; int sum = 0; @@ -41,22 +42,6 @@ public class HashTable { return (Math.abs(sum) % tamano); } - /** - * Este metodo crea un hash usando una llave. - * @param string String: El string a hashear. - * @return int: El hash a devolver. - */ - public int hash(String string) { - int hash = 31; - for (int i = 0; i < string.length(); i++) { - hash = hash * 31 + string.charAt(i); - } - if (hash < 0) { - hash = hash * -1; - } - return hash % tamano; - } - public boolean insertar(String llave, int valor) { HashItem hashItem = new HashItem(llave, valor); int hashIndice = hash(hashItem.getLlave()); @@ -80,6 +65,24 @@ public class HashTable { } } + /** + * Este metodo crea un hash usando una llave. + * + * @param string String: El string a hashear. + * + * @return int: El hash a devolver. + */ + public int hash(String string) { + int hash = 31; + for (int i = 0; i < string.length(); i++) { + hash = hash * 31 + string.charAt(i); + } + if (hash < 0) { + hash = hash * - 1; + } + return hash % tamano; + } + public boolean eliminar(String llave) { HashItem hashItem = new HashItem(llave, 0); int hashIndice = hash(hashItem.getLlave()); diff --git a/src/cl/cromer/estructuras/InsercionController.java b/src/cl/cromer/estructuras/InsercionController.java index 881cf72..d7225b2 100644 --- a/src/cl/cromer/estructuras/InsercionController.java +++ b/src/cl/cromer/estructuras/InsercionController.java @@ -7,7 +7,6 @@ import javafx.scene.layout.HBox; import javafx.scene.text.Text; import java.net.URL; -import java.util.Random; import java.util.ResourceBundle; import java.util.Scanner; @@ -51,6 +50,7 @@ public class InsercionController implements Initializable { * @param resourceBundle ResourceBundle: Tiene datos de idioma. */ @Override + @SuppressWarnings("Duplicates") public void initialize(URL location, ResourceBundle resourceBundle) { this.resourceBundle = resourceBundle; @@ -58,21 +58,12 @@ public class InsercionController implements Initializable { Colores colores = new Colores(); - Random random = new Random(); - int maximo = 99; - int minimo = 0; - int rango = maximo - minimo + 1; - array = new Array(10); array.setOrdered(true); + array.llenar(); for (int i = 0; i < 10; i++) { - int numero = random.nextInt(rango) + minimo; - while (array.buscar(numero) != -1) { - numero = random.nextInt(rango) + minimo; - } - array.insertar(numero); - contenidoInsercion.getChildren().addAll(Grafico.crearCaja(colores, String.valueOf(i), String.valueOf(numero))); + contenidoInsercion.getChildren().addAll(Grafico.crearCaja(colores, String.valueOf(i), String.valueOf(array.getIndice(i)))); colores.siguinteColor(); } } @@ -86,24 +77,28 @@ public class InsercionController implements Initializable { initializeScene(); } - array = new Array(10); - array.setOrdered(true); - - Random random = new Random(); - int maximo = 99; - int minimo = 0; - int rango = maximo - minimo + 1; - - for (int i = array.size(); i < 10; i++) { - int numero = random.nextInt(rango) + minimo; - while (array.buscar(numero) != -1) { - numero = random.nextInt(rango) + minimo; - } - array.insertar(numero); - } + array.nuevo(); + array.llenar(); generarGrafico(); } + /** + * Crear el array de tamaño 10. + */ + private void initializeScene() { + scene = contenidoInsercion.getScene(); + } + + /** + * Poner los valores en el grafico. + */ + private void generarGrafico() { + for (int i = 0; i < 10; i++) { + Text text = (Text) scene.lookup("#texto_" + String.valueOf(i)); + text.setText(array.getIndice(i)); + } + } + /** * Ordenarlo paso por paso. */ @@ -143,21 +138,4 @@ public class InsercionController implements Initializable { generarGrafico(); } - - /** - * Crear el array de tamaño 10. - */ - private void initializeScene() { - scene = contenidoInsercion.getScene(); - } - - /** - * Poner los valores en el grafico. - */ - private void generarGrafico() { - for (int i = 0; i < 10; i++) { - Text text = (Text) scene.lookup("#texto_" + String.valueOf(i)); - text.setText(array.getIndice(i)); - } - } } diff --git a/src/cl/cromer/estructuras/ListaEnlazdaController.java b/src/cl/cromer/estructuras/ListaEnlazdaController.java index 29fef5c..1042cc7 100644 --- a/src/cl/cromer/estructuras/ListaEnlazdaController.java +++ b/src/cl/cromer/estructuras/ListaEnlazdaController.java @@ -126,14 +126,57 @@ public class ListaEnlazdaController implements Initializable { } /** - * Vaciar la lista de todos los valores. + * Crear una lista vacia. */ - @FXML - protected void botonVaciar() { - if (scene == null) { - initializeLista(); - } + private void initializeLista() { + scene = contenidoLista.getScene(); + grafico = new Grafico(scene); + listaEnlazadaTipos = (ListaEnlazadaTipos) scene.getUserData(); + nuevaLista(); + } + /** + * Poner los valores en el grafico. + */ + private void generarGrafico() { + grafico.removerDestacar(); + colores = new Colores(); + contenidoLista.getChildren().clear(); + contenidoListaCircular.getChildren().clear(); + + if (listaEnlazadaTipos.getTipo() != ListaEnlazadaTipos.CIRCULAR) { + for (int i = 0; i < listaEnlazada.size(); i++) { + Enlace enlace = listaEnlazada.getIndice(i); + if (listaEnlazada.getTipo() == ListaEnlazadaTipos.SIMPLE) { + dibujarSimple(enlace, false); + } + else if (listaEnlazada.getTipo() == ListaEnlazadaTipos.DOBLEMENTE_ENLAZADA) { + if (i != listaEnlazada.size() - 1) { + dibujarDoble(enlace, (i == 0)); + } + else { + dibujarSimple(enlace, false); + } + } + colores.siguinteColor(); + } + } + else { + for (int i = 0; i < listaEnlazadaCircular.size(); i++) { + Enlace enlace = listaEnlazadaCircular.getIndice(i); + dibujarSimple(enlace, (i == listaEnlazadaCircular.size() - 1)); + colores.siguinteColor(); + } + if (listaEnlazadaCircular.size() > 0) { + contenidoListaCircular.getChildren().addAll(Grafico.crearLineaCircular(listaEnlazadaCircular.size())); + } + } + } + + /** + * Crear una nueva lista enlazada. + */ + private void nuevaLista() { if (listaEnlazadaTipos.getTipo() != ListaEnlazadaTipos.CIRCULAR) { listaEnlazada = new ListaEnlazada(); listaEnlazada.setTipo(listaEnlazadaTipos.getTipo()); @@ -142,6 +185,57 @@ public class ListaEnlazdaController implements Initializable { listaEnlazadaCircular = new ListaEnlazadaCircular(); listaEnlazadaCircular.setTipo(ListaEnlazadaTipos.SIMPLE); } + } + + /** + * Dibujarlo con una flecha. + * + * @param enlace Enlace: El enlace que tiene la llave y valor. + * @param sinFlecha boolean: Verdad si necesita dibujar una flecha. + */ + private void dibujarSimple(Enlace enlace, boolean sinFlecha) { + contenidoLista.getChildren().addAll( + Grafico.crearCaja(colores, String.valueOf(enlace.getLlave()), String.valueOf(enlace.getLlave())) + ); + if (! sinFlecha) { + contenidoLista.getChildren().addAll( + Grafico.crearLineaVertical(), + Grafico.crearFlechaAbajo() + ); + } + } + + /** + * Dibujarlo con dos flechas. + * + * @param enlace Enlace: El enlace que tiene la llave y valor. + * @param primer boolean: Verdad si es el primer elemento de la lista. + */ + private void dibujarDoble(Enlace enlace, boolean primer) { + if (primer) { + contenidoLista.getChildren().addAll( + Grafico.crearFlechaArriba(), + Grafico.crearLineaVertical() + ); + } + contenidoLista.getChildren().addAll( + Grafico.crearCaja(colores, String.valueOf(enlace.getLlave()), String.valueOf(enlace.getLlave())), + Grafico.crearFlechaArriba(), + Grafico.crearLineaVertical(), + Grafico.crearFlechaAbajo() + ); + } + + /** + * Vaciar la lista de todos los valores. + */ + @FXML + protected void botonVaciar() { + if (scene == null) { + initializeLista(); + } + + nuevaLista(); generarGrafico(); } @@ -154,20 +248,7 @@ public class ListaEnlazdaController implements Initializable { initializeLista(); } - String tipo; - switch (listaEnlazadaTipos.getTipo()) { - case ListaEnlazadaTipos.SIMPLE: - tipo = "Simple"; - break; - case ListaEnlazadaTipos.CIRCULAR: - tipo = "Circular"; - break; - case ListaEnlazadaTipos.DOBLEMENTE_ENLAZADA: - tipo = "Doble"; - break; - default: - tipo = "Simple"; - } + String tipo = getTipoString(); // Mostrar el codigo String codigoTexto = new Scanner(getClass().getResourceAsStream("/cl/cromer/estructuras/code/listaEnlazada" + tipo + "/insertar")).useDelimiter("\\Z").next(); @@ -201,15 +282,7 @@ public class ListaEnlazdaController implements Initializable { } } - /** - * Eliminar un valor de la lista si existe y mostrar el codigo en la pantalla. - */ - @FXML - protected void botonEliminar() { - if (scene == null) { - initializeLista(); - } - + private String getTipoString() { String tipo; switch (listaEnlazadaTipos.getTipo()) { case ListaEnlazadaTipos.SIMPLE: @@ -224,6 +297,19 @@ public class ListaEnlazdaController implements Initializable { default: tipo = "Simple"; } + return tipo; + } + + /** + * Eliminar un valor de la lista si existe y mostrar el codigo en la pantalla. + */ + @FXML + protected void botonEliminar() { + if (scene == null) { + initializeLista(); + } + + String tipo = getTipoString(); // Mostrar el codigo String codigoTexto = new Scanner(getClass().getResourceAsStream("/cl/cromer/estructuras/code/listaEnlazada" + tipo + "/eliminar")).useDelimiter("\\Z").next(); @@ -267,20 +353,7 @@ public class ListaEnlazdaController implements Initializable { initializeLista(); } - String tipo; - switch (listaEnlazadaTipos.getTipo()) { - case ListaEnlazadaTipos.SIMPLE: - tipo = "Simple"; - break; - case ListaEnlazadaTipos.CIRCULAR: - tipo = "Circular"; - break; - case ListaEnlazadaTipos.DOBLEMENTE_ENLAZADA: - tipo = "Doble"; - break; - default: - tipo = "Simple"; - } + String tipo = getTipoString(); // Mostrar el codigo String codigoTexto = new Scanner(getClass().getResourceAsStream("/cl/cromer/estructuras/code/listaEnlazada" + tipo + "/buscar")).useDelimiter("\\Z").next(); @@ -315,98 +388,4 @@ public class ListaEnlazdaController implements Initializable { Main.mostrarError(resourceBundle.getString("listaNoValor"), resourceBundle); } } - - /** - * Crear una lista vacia. - */ - private void initializeLista() { - scene = contenidoLista.getScene(); - grafico = new Grafico(scene); - listaEnlazadaTipos = (ListaEnlazadaTipos) scene.getUserData(); - if (listaEnlazadaTipos.getTipo() != ListaEnlazadaTipos.CIRCULAR) { - listaEnlazada = new ListaEnlazada(); - listaEnlazada.setTipo(listaEnlazadaTipos.getTipo()); - } - else { - listaEnlazadaCircular = new ListaEnlazadaCircular(); - listaEnlazadaCircular.setTipo(ListaEnlazadaTipos.SIMPLE); - } - } - - /** - * Poner los valores en el grafico. - */ - private void generarGrafico() { - grafico.removerDestacar(); - colores = new Colores(); - contenidoLista.getChildren().clear(); - contenidoListaCircular.getChildren().clear(); - - if (listaEnlazadaTipos.getTipo() != ListaEnlazadaTipos.CIRCULAR) { - for (int i = 0; i < listaEnlazada.size(); i++) { - Enlace enlace = listaEnlazada.getIndice(i); - if (listaEnlazada.getTipo() == ListaEnlazadaTipos.SIMPLE) { - dibujarSimple(enlace, false); - } - else if (listaEnlazada.getTipo() == ListaEnlazadaTipos.DOBLEMENTE_ENLAZADA) { - if (i != listaEnlazada.size() - 1) { - dibujarDoble(enlace, (i == 0)); - } - else { - dibujarSimple(enlace, false); - } - } - colores.siguinteColor(); - } - } - else { - for (int i = 0; i < listaEnlazadaCircular.size(); i++) { - Enlace enlace = listaEnlazadaCircular.getIndice(i); - dibujarSimple(enlace, (i == listaEnlazadaCircular.size() - 1)); - colores.siguinteColor(); - } - if (listaEnlazadaCircular.size() > 0) { - contenidoListaCircular.getChildren().addAll(Grafico.crearLineaCircular(listaEnlazadaCircular.size())); - } - } - } - - /** - * Dibujarlo con una flecha. - * - * @param enlace Enlace: El enlace que tiene la llave y valor. - * @param sinFlecha boolean: Verdad si necesita dibujar una flecha. - */ - private void dibujarSimple(Enlace enlace, boolean sinFlecha) { - contenidoLista.getChildren().addAll( - Grafico.crearCaja(colores, String.valueOf(enlace.getLlave()), String.valueOf(enlace.getLlave())) - ); - if (!sinFlecha) { - contenidoLista.getChildren().addAll( - Grafico.crearLineaVertical(), - Grafico.crearFlechaAbajo() - ); - } - } - - /** - * Dibujarlo con dos flechas. - * - * @param enlace Enlace: El enlace que tiene la llave y valor. - * @param primer boolean: Verdad si es el primer elemento de la lista. - */ - private void dibujarDoble(Enlace enlace, boolean primer) { - if (primer) { - contenidoLista.getChildren().addAll( - Grafico.crearFlechaArriba(), - Grafico.crearLineaVertical() - ); - } - contenidoLista.getChildren().addAll( - Grafico.crearCaja(colores, String.valueOf(enlace.getLlave()), String.valueOf(enlace.getLlave())), - Grafico.crearFlechaArriba(), - Grafico.crearLineaVertical(), - Grafico.crearFlechaAbajo() - ); - } } diff --git a/src/cl/cromer/estructuras/MenuController.java b/src/cl/cromer/estructuras/MenuController.java index 3cd099c..c7866cf 100644 --- a/src/cl/cromer/estructuras/MenuController.java +++ b/src/cl/cromer/estructuras/MenuController.java @@ -61,6 +61,38 @@ public class MenuController extends VBox implements Initializable { ); } + /** + * Cargar el fxml, css y titulo. + * + * @param title String: El titulo de la escena. + * @param fxml String: El archivo de fxml. + * @param css String: El archivo de css. + * @param object Object: El objeto a pasar a la nueva escena. + */ + private void loadStage(String title, String fxml, String css, Object object) { + Scene scene = menuBar.getScene(); + Stage stage = (Stage) scene.getWindow(); + + openFXML(fxml, scene, stage); + + scene.getStylesheets().add(css); + scene.setUserData(object); + stage.setScene(scene); + stage.setTitle(this.resourceBundle.getString("titulo") + " - " + title); + } + + private void openFXML(String fxml, Scene scene, Stage stage) { + try { + Parent parent = FXMLLoader.load(getClass().getResource(fxml), this.resourceBundle); + scene.setRoot(parent); + } + catch (IOException exception) { + // Este error es fatal, hay que cerrar la aplicación. + Logs.log(Level.SEVERE, "No se pudo abrir el archivo de fxml."); + stage.close(); + } + } + /** * Click en Array Ordenado. */ @@ -87,6 +119,24 @@ public class MenuController extends VBox implements Initializable { ); } + /** + * Cargar el fxml, css y titulo. + * + * @param title String: El titulo de la escena. + * @param fxml String: El archivo de fxml. + * @param css String: El archivo de css. + */ + private void loadStage(String title, String fxml, String css) { + Scene scene = menuBar.getScene(); + Stage stage = (Stage) scene.getWindow(); + + openFXML(fxml, scene, stage); + + scene.getStylesheets().add(css); + stage.setScene(scene); + stage.setTitle(this.resourceBundle.getString("titulo") + " - " + title); + } + /** * Click en Inserción. */ @@ -254,6 +304,32 @@ public class MenuController extends VBox implements Initializable { } } + /** + * Cargar el fxml y css. + * + * @param fxml String: El archivo de fxml. + * @param css String: El archivo de css. + * @param resourceBundle ResourceBundle: El idioma nuevo para cambiarlo. + */ + private void loadStage(String fxml, String css, ResourceBundle resourceBundle) { + Scene scene = menuBar.getScene(); + Stage stage = (Stage) scene.getWindow(); + + try { + Parent parent = FXMLLoader.load(getClass().getResource(fxml), resourceBundle); + scene.setRoot(parent); + } + catch (IOException exception) { + // Este error es fatal, hay que cerrar la aplicación. + Logs.log(Level.SEVERE, "No se pudo abrir el archivo de fxml."); + stage.close(); + } + + scene.getStylesheets().add(css); + stage.setScene(scene); + stage.setTitle(resourceBundle.getString("titulo")); + } + /** * Click en Español. */ @@ -297,84 +373,4 @@ public class MenuController extends VBox implements Initializable { Main.setIcon(dialog, getClass()); dialog.show(); } - - /** - * Cargar el fxml, css y titulo. - * - * @param title String: El titulo de la escena. - * @param fxml String: El archivo de fxml. - * @param css String: El archivo de css. - */ - private void loadStage(String title, String fxml, String css) { - Scene scene = menuBar.getScene(); - Stage stage = (Stage) scene.getWindow(); - - try { - Parent parent = FXMLLoader.load(getClass().getResource(fxml), resourceBundle); - scene.setRoot(parent); - } - catch (IOException exception) { - // Este error es fatal, hay que cerrar la aplicación. - Logs.log(Level.SEVERE, "No se pudo abrir el archivo de fxml."); - stage.close(); - } - - scene.getStylesheets().add(css); - stage.setScene(scene); - stage.setTitle(resourceBundle.getString("titulo") + " - " + title); - } - - /** - * Cargar el fxml y css. - * - * @param fxml String: El archivo de fxml. - * @param css String: El archivo de css. - * @param resourceBundle ResourceBundle: El idioma nuevo para cambiarlo. - */ - private void loadStage(String fxml, String css, ResourceBundle resourceBundle) { - Scene scene = menuBar.getScene(); - Stage stage = (Stage) scene.getWindow(); - - try { - Parent parent = FXMLLoader.load(getClass().getResource(fxml), resourceBundle); - scene.setRoot(parent); - } - catch (IOException exception) { - // Este error es fatal, hay que cerrar la aplicación. - Logs.log(Level.SEVERE, "No se pudo abrir el archivo de fxml."); - stage.close(); - } - - scene.getStylesheets().add(css); - stage.setScene(scene); - stage.setTitle(resourceBundle.getString("titulo")); - } - - /** - * Cargar el fxml, css y titulo. - * - * @param title String: El titulo de la escena. - * @param fxml String: El archivo de fxml. - * @param css String: El archivo de css. - * @param object Object: El objeto a pasar a la nueva escena. - */ - private void loadStage(String title, String fxml, String css, Object object) { - Scene scene = menuBar.getScene(); - Stage stage = (Stage) scene.getWindow(); - - try { - Parent parent = FXMLLoader.load(getClass().getResource(fxml), resourceBundle); - scene.setRoot(parent); - } - catch (IOException exception) { - // Este error es fatal, hay que cerrar la aplicación. - Logs.log(Level.SEVERE, "No se pudo abrir el archivo de fxml."); - stage.close(); - } - - scene.getStylesheets().add(css); - scene.setUserData(object); - stage.setScene(scene); - stage.setTitle(resourceBundle.getString("titulo") + " - " + title); - } } \ No newline at end of file diff --git a/src/cl/cromer/estructuras/MergeController.java b/src/cl/cromer/estructuras/MergeController.java index 03649ac..23d86e7 100644 --- a/src/cl/cromer/estructuras/MergeController.java +++ b/src/cl/cromer/estructuras/MergeController.java @@ -7,7 +7,6 @@ import javafx.scene.layout.HBox; import javafx.scene.text.Text; import java.net.URL; -import java.util.Random; import java.util.ResourceBundle; import java.util.Scanner; @@ -51,6 +50,7 @@ public class MergeController implements Initializable { * @param resourceBundle ResourceBundle: Tiene datos de idioma. */ @Override + @SuppressWarnings("Duplicates") public void initialize(URL location, ResourceBundle resourceBundle) { this.resourceBundle = resourceBundle; @@ -58,21 +58,12 @@ public class MergeController implements Initializable { Colores colores = new Colores(); - Random random = new Random(); - int maximo = 99; - int minimo = 0; - int rango = maximo - minimo + 1; - array = new Array(10); array.setOrdered(true); + array.llenar(); for (int i = 0; i < 10; i++) { - int numero = random.nextInt(rango) + minimo; - while (array.buscar(numero) != -1) { - numero = random.nextInt(rango) + minimo; - } - array.insertar(numero); - contenidoMerge.getChildren().addAll(Grafico.crearCaja(colores, String.valueOf(i), String.valueOf(numero))); + contenidoMerge.getChildren().addAll(Grafico.crearCaja(colores, String.valueOf(i), String.valueOf(array.getIndice(i)))); colores.siguinteColor(); } } @@ -86,24 +77,28 @@ public class MergeController implements Initializable { initializeScene(); } - array = new Array(10); - array.setOrdered(true); - - Random random = new Random(); - int maximo = 99; - int minimo = 0; - int rango = maximo - minimo + 1; - - for (int i = array.size(); i < 10; i++) { - int numero = random.nextInt(rango) + minimo; - while (array.buscar(numero) != -1) { - numero = random.nextInt(rango) + minimo; - } - array.insertar(numero); - } + array.nuevo(); + array.llenar(); generarGrafico(); } + /** + * Crear el array de tamaño 10. + */ + private void initializeScene() { + scene = contenidoMerge.getScene(); + } + + /** + * Poner los valores en el grafico. + */ + private void generarGrafico() { + for (int i = 0; i < 10; i++) { + Text text = (Text) scene.lookup("#texto_" + String.valueOf(i)); + text.setText(array.getIndice(i)); + } + } + /** * Ordenarlo paso por paso. */ @@ -143,21 +138,4 @@ public class MergeController implements Initializable { generarGrafico(); } - - /** - * Crear el array de tamaño 10. - */ - private void initializeScene() { - scene = contenidoMerge.getScene(); - } - - /** - * Poner los valores en el grafico. - */ - private void generarGrafico() { - for (int i = 0; i < 10; i++) { - Text text = (Text) scene.lookup("#texto_" + String.valueOf(i)); - text.setText(array.getIndice(i)); - } - } } diff --git a/src/cl/cromer/estructuras/Pila.java b/src/cl/cromer/estructuras/Pila.java index 82ab536..87133a6 100644 --- a/src/cl/cromer/estructuras/Pila.java +++ b/src/cl/cromer/estructuras/Pila.java @@ -1,5 +1,7 @@ package cl.cromer.estructuras; +import java.util.Random; + /** * Crear una estructura de dato de tipo pila. * @@ -24,39 +26,6 @@ final public class Pila { size = 0; } - /** - * Devolver la cantidad de elementos en la pila. - * - * @return int: La cantidad de elementos. - */ - public int size() { - return size; - } - - /** - * Push un valor en la pila encima. - * - * @param valor int: El valor a push. - */ - public void push(int valor) { - if (this.pila != null) { - String pila[] = new String[this.pila.length + 1]; - int i; - for (i = 0; i < this.pila.length; i++) { - pila[i] = this.pila[i]; - } - pila[i] = String.valueOf(valor); - this.pila = pila; - size++; - } - else { - String pila[] = new String[1]; - pila[0] = String.valueOf(valor); - this.pila = pila; - size++; - } - } - /** * Pop un valor de encima de la pila. * @@ -75,6 +44,15 @@ final public class Pila { } } + /** + * Devolver la cantidad de elementos en la pila. + * + * @return int: La cantidad de elementos. + */ + public int size() { + return size; + } + /** * Peek al valor que está encima de la pila. * @@ -103,4 +81,44 @@ final public class Pila { return null; } } + + /** + * Llenar la pila con valores al azar. + */ + @SuppressWarnings("Duplicates") + public void llenar() { + Random random = new Random(); + int maximo = 99; + int minimo = 0; + int rango = maximo - minimo + 1; + + for (int i = size(); i < 10; i++) { + int numero = random.nextInt(rango) + minimo; + push(numero); + } + } + + /** + * Push un valor en la pila encima. + * + * @param valor int: El valor a push. + */ + public void push(int valor) { + if (this.pila != null) { + String pila[] = new String[this.pila.length + 1]; + int i; + for (i = 0; i < this.pila.length; i++) { + pila[i] = this.pila[i]; + } + pila[i] = String.valueOf(valor); + this.pila = pila; + size++; + } + else { + String pila[] = new String[1]; + pila[0] = String.valueOf(valor); + this.pila = pila; + size++; + } + } } \ No newline at end of file diff --git a/src/cl/cromer/estructuras/PilaController.java b/src/cl/cromer/estructuras/PilaController.java index 5f6e983..1e7da50 100644 --- a/src/cl/cromer/estructuras/PilaController.java +++ b/src/cl/cromer/estructuras/PilaController.java @@ -7,7 +7,6 @@ import javafx.scene.layout.VBox; import javafx.scene.text.Text; import java.net.URL; -import java.util.Random; import java.util.ResourceBundle; import java.util.Scanner; import java.util.logging.Level; @@ -87,18 +86,21 @@ public class PilaController implements Initializable { grafico = new Grafico(scene); } - Random random = new Random(); - int maximo = 99; - int minimo = 0; - int rango = maximo - minimo + 1; - - for (int i = pila.size(); i < 10; i++) { - int numero = random.nextInt(rango) + minimo; - pila.push(numero); - } + pila.llenar(); generarGrafico(); } + /** + * Poner los valores en el grafico. + */ + private void generarGrafico() { + grafico.removerDestacar(); + for (int i = 0; i < 10; i++) { + Text text = (Text) scene.lookup("#texto_" + String.valueOf(i)); + text.setText(pila.getIndice(i)); + } + } + /** * Vaciar la pila de todos los valores. */ @@ -201,15 +203,4 @@ public class PilaController implements Initializable { Main.mostrarError(resourceBundle.getString("pilaVacia"), resourceBundle); } } - - /** - * Poner los valores en el grafico. - */ - private void generarGrafico() { - grafico.removerDestacar(); - for (int i = 0; i < 10; i++) { - Text text = (Text) scene.lookup("#texto_" + String.valueOf(i)); - text.setText(pila.getIndice(i)); - } - } } \ No newline at end of file diff --git a/src/cl/cromer/estructuras/QuickController.java b/src/cl/cromer/estructuras/QuickController.java index b2ff7f1..78f1077 100644 --- a/src/cl/cromer/estructuras/QuickController.java +++ b/src/cl/cromer/estructuras/QuickController.java @@ -7,7 +7,6 @@ import javafx.scene.layout.HBox; import javafx.scene.text.Text; import java.net.URL; -import java.util.Random; import java.util.ResourceBundle; import java.util.Scanner; @@ -51,6 +50,7 @@ public class QuickController implements Initializable { * @param resourceBundle ResourceBundle: Tiene datos de idioma. */ @Override + @SuppressWarnings("Duplicates") public void initialize(URL location, ResourceBundle resourceBundle) { this.resourceBundle = resourceBundle; @@ -58,21 +58,12 @@ public class QuickController implements Initializable { Colores colores = new Colores(); - Random random = new Random(); - int maximo = 99; - int minimo = 0; - int rango = maximo - minimo + 1; - array = new Array(10); array.setOrdered(true); + array.llenar(); for (int i = 0; i < 10; i++) { - int numero = random.nextInt(rango) + minimo; - while (array.buscar(numero) != -1) { - numero = random.nextInt(rango) + minimo; - } - array.insertar(numero); - contenidoQuick.getChildren().addAll(Grafico.crearCaja(colores, String.valueOf(i), String.valueOf(numero))); + contenidoQuick.getChildren().addAll(Grafico.crearCaja(colores, String.valueOf(i), String.valueOf(array.getIndice(i)))); colores.siguinteColor(); } } @@ -86,24 +77,28 @@ public class QuickController implements Initializable { initializeScene(); } - array = new Array(10); - array.setOrdered(true); - - Random random = new Random(); - int maximo = 99; - int minimo = 0; - int rango = maximo - minimo + 1; - - for (int i = array.size(); i < 10; i++) { - int numero = random.nextInt(rango) + minimo; - while (array.buscar(numero) != -1) { - numero = random.nextInt(rango) + minimo; - } - array.insertar(numero); - } + array.nuevo(); + array.llenar(); generarGrafico(); } + /** + * Crear el array de tamaño 10. + */ + private void initializeScene() { + scene = contenidoQuick.getScene(); + } + + /** + * Poner los valores en el grafico. + */ + private void generarGrafico() { + for (int i = 0; i < 10; i++) { + Text text = (Text) scene.lookup("#texto_" + String.valueOf(i)); + text.setText(array.getIndice(i)); + } + } + /** * Ordenarlo paso por paso. */ @@ -143,21 +138,4 @@ public class QuickController implements Initializable { generarGrafico(); } - - /** - * Crear el array de tamaño 10. - */ - private void initializeScene() { - scene = contenidoQuick.getScene(); - } - - /** - * Poner los valores en el grafico. - */ - private void generarGrafico() { - for (int i = 0; i < 10; i++) { - Text text = (Text) scene.lookup("#texto_" + String.valueOf(i)); - text.setText(array.getIndice(i)); - } - } } diff --git a/src/cl/cromer/estructuras/SeleccionController.java b/src/cl/cromer/estructuras/SeleccionController.java index c3bd71f..ff6803d 100644 --- a/src/cl/cromer/estructuras/SeleccionController.java +++ b/src/cl/cromer/estructuras/SeleccionController.java @@ -7,7 +7,6 @@ import javafx.scene.layout.HBox; import javafx.scene.text.Text; import java.net.URL; -import java.util.Random; import java.util.ResourceBundle; import java.util.Scanner; @@ -51,6 +50,7 @@ public class SeleccionController implements Initializable { * @param resourceBundle ResourceBundle: Tiene datos de idioma. */ @Override + @SuppressWarnings("Duplicates") public void initialize(URL location, ResourceBundle resourceBundle) { this.resourceBundle = resourceBundle; @@ -58,22 +58,13 @@ public class SeleccionController implements Initializable { Colores colores = new Colores(); - Random random = new Random(); - int maximo = 99; - int minimo = 0; - int rango = maximo - minimo + 1; - array = new Array(10); array.setOrdered(true); + array.llenar(); for (int i = 0; i < 10; i++) { - int numero = random.nextInt(rango) + minimo; - while (array.buscar(numero) != -1) { - numero = random.nextInt(rango) + minimo; - } - array.insertar(numero); - contenidoSeleccion.getChildren().addAll(Grafico.crearCaja(colores, String.valueOf(i), String.valueOf(numero))); - colores.siguinteColor(); + contenidoSeleccion.getChildren().addAll(Grafico.crearCaja(colores, String.valueOf(i), String.valueOf(array.getIndice(i)))); + colores.siguinteColor(); } } @@ -86,25 +77,29 @@ public class SeleccionController implements Initializable { initializeScene(); } - array = new Array(10); - array.setOrdered(true); - - Random random = new Random(); - int maximo = 99; - int minimo = 0; - int rango = maximo - minimo + 1; - - for (int i = array.size(); i < 10; i++) { - int numero = random.nextInt(rango) + minimo; - while (array.buscar(numero) != -1) { - numero = random.nextInt(rango) + minimo; - } - array.insertar(numero); - } - generarGrafico(); + array.nuevo(); + array.llenar(); + generarGrafico(); } /** + * Crear el array de tamaño 10. + */ + private void initializeScene() { + scene = contenidoSeleccion.getScene(); + } + + /** + * Poner los valores en el grafico. + */ + private void generarGrafico() { + for (int i = 0; i < 10; i++) { + Text text = (Text) scene.lookup("#texto_" + String.valueOf(i)); + text.setText(array.getIndice(i)); + } + } + + /** * Ordenarlo paso por paso. */ @FXML @@ -143,21 +138,4 @@ public class SeleccionController implements Initializable { generarGrafico(); } - - /** - * Crear el array de tamaño 10. - */ - private void initializeScene() { - scene = contenidoSeleccion.getScene(); - } - - /** - * Poner los valores en el grafico. - */ - private void generarGrafico() { - for (int i = 0; i < 10; i++) { - Text text = (Text) scene.lookup("#texto_" + String.valueOf(i)); - text.setText(array.getIndice(i)); - } - } } diff --git a/src/cl/cromer/estructuras/ShellController.java b/src/cl/cromer/estructuras/ShellController.java index 71a436f..cb3baec 100644 --- a/src/cl/cromer/estructuras/ShellController.java +++ b/src/cl/cromer/estructuras/ShellController.java @@ -7,7 +7,6 @@ import javafx.scene.layout.HBox; import javafx.scene.text.Text; import java.net.URL; -import java.util.Random; import java.util.ResourceBundle; import java.util.Scanner; @@ -47,10 +46,11 @@ public class ShellController implements Initializable { /** * Inicializar todos los datos y dibujar las graficas. * - * @param location URL: El URL de fxml en uso. + * @param location URL: El URL de fxml en uso. * @param resourceBundle ResourceBundle: Tiene datos de idioma. */ @Override + @SuppressWarnings("Duplicates") public void initialize(URL location, ResourceBundle resourceBundle) { this.resourceBundle = resourceBundle; @@ -58,22 +58,13 @@ public class ShellController implements Initializable { Colores colores = new Colores(); - Random random = new Random(); - int maximo = 99; - int minimo = 0; - int rango = maximo - minimo + 1; - array = new Array(10); array.setOrdered(true); + array.llenar(); for (int i = 0; i < 10; i++) { - int numero = random.nextInt(rango) + minimo; - while (array.buscar(numero) != -1) { - numero = random.nextInt(rango) + minimo; - } - array.insertar(numero); - contenidoShell.getChildren().addAll(Grafico.crearCaja(colores, String.valueOf(i), String.valueOf(numero))); - colores.siguinteColor(); + contenidoShell.getChildren().addAll(Grafico.crearCaja(colores, String.valueOf(i), String.valueOf(array.getIndice(i)))); + colores.siguinteColor(); } } @@ -86,25 +77,29 @@ public class ShellController implements Initializable { initializeScene(); } - array = new Array(10); - array.setOrdered(true); - - Random random = new Random(); - int maximo = 99; - int minimo = 0; - int rango = maximo - minimo + 1; - - for (int i = array.size(); i < 10; i++) { - int numero = random.nextInt(rango) + minimo; - while (array.buscar(numero) != -1) { - numero = random.nextInt(rango) + minimo; - } - array.insertar(numero); - } - generarGrafico(); + array.nuevo(); + array.llenar(); + generarGrafico(); } /** + * Crear el array de tamaño 10. + */ + private void initializeScene() { + scene = contenidoShell.getScene(); + } + + /** + * Poner los valores en el grafico. + */ + private void generarGrafico() { + for (int i = 0; i < 10; i++) { + Text text = (Text) scene.lookup("#texto_" + String.valueOf(i)); + text.setText(array.getIndice(i)); + } + } + + /** * Ordenarlo paso por paso. */ @FXML @@ -143,21 +138,4 @@ public class ShellController implements Initializable { generarGrafico(); } - - /** - * Crear el array de tamaño 10. - */ - private void initializeScene() { - scene = contenidoShell.getScene(); - } - - /** - * Poner los valores en el grafico. - */ - private void generarGrafico() { - for (int i = 0; i < 10; i++) { - Text text = (Text) scene.lookup("#texto_" + String.valueOf(i)); - text.setText(array.getIndice(i)); - } - } }