Created
May 23, 2021 17:01
-
-
Save arellano-gustavo/adada95d033252a6961a643a65b9609e to your computer and use it in GitHub Desktop.
Ejercicio de nodos en Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mx.com.ultrasist.ci.generator; | |
// Curly brace Enemy ... | |
// https://javarevisited.blogspot.com/2017/03/how-to-reverse-linked-list-in-java-using-iteration-and-recursion.html | |
public class SinglyLinkedList<T> { | |
private Node<T> head; | |
/** | |
* Imprime esta lista ligada con un formato | |
* específico delimitado por símbolos '--->' | |
*/ | |
@Override | |
public String toString() { | |
StringBuilder sb = new StringBuilder(); | |
Node<T> current = head; | |
while (current != null) { | |
sb.append(current).append("-->"); | |
current = current.next; | |
} | |
// to remove --> from last node: | |
if (sb.length() >= 3) { | |
sb.delete(sb.length() - 3, sb.length()); | |
} | |
return sb.toString(); | |
} | |
/** | |
* Crea una lista ligada de objetos de topo 'Node' con alguna | |
* letra (seleccionada del conjunto 'ABCDEFghijk') como el | |
* contenido de cada nodo. | |
*/ | |
private static SinglyLinkedList<String> createList() { | |
String source = "ABCDEFghijk"; | |
SinglyLinkedList<String> linkedlist = new SinglyLinkedList<>(); | |
for (int i = 0; i < source.length(); i++) | |
linkedlist.append(source.charAt(i) + ""); | |
return linkedlist; | |
} | |
/** | |
* Agrega a una lista ligada un elemento al final de ella | |
*/ | |
public void append(T data) { | |
if (head == null) { | |
head = new Node<T>(data); | |
} else { | |
tail().next = new Node<T>(data); | |
} | |
} | |
/** | |
* Auxiliar del método 'append'. Regresa el último elemento de una lista | |
*/ | |
private Node<T> tail() { | |
Node<T> tail = head; | |
while (tail.next != null) tail = tail.next; | |
return tail; | |
} | |
/** | |
* AQUI DEBES ADIVINAR QUÉ HACE ESTE MÉTODO LLAMADO 'misterioso' | |
* PERO TAMBIÉN DEBES DECIR PORQUE LO HACE Y EXPLICAR SU ALGORITMO. | |
*/ | |
private Node<T> misterioso(Node<T> tavo) { | |
if (tavo.next == null) return tavo; | |
Node<T> gus = misterioso(tavo.next); | |
tavo.next.next = tavo; | |
tavo.next = null; | |
return gus; | |
} | |
public void adivina() { | |
head = misterioso(head); | |
} | |
/** | |
* Definición de la clase privada 'Node' | |
*/ | |
private static class Node<T> { | |
private Node<T> next; | |
private T data; | |
public Node(T data) { | |
this.data = data; | |
} | |
public String toString() { | |
return data.toString(); | |
} | |
} | |
/** | |
* Ejecuta el programa | |
*/ | |
public static void main(String args[]) { | |
SinglyLinkedList<String> linkedlist = createList(); | |
System.out.println("linked list before adivina : " + linkedlist); | |
linkedlist.adivina(); | |
System.out.println("linked list after adivina " + linkedlist); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
El chiste es "entender" el funcionamiento del método "misterioso"....
Buena suerte...