Restructures demos.
This commit is contained in:
58
demos/java/src/app/App.java
Normal file
58
demos/java/src/app/App.java
Normal file
@@ -0,0 +1,58 @@
|
||||
package app;
|
||||
|
||||
import app.GraphData.EdgeInfo;
|
||||
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// watch `list.visualize()`
|
||||
LinkedList<String> list = new LinkedList<String>();
|
||||
list.append("1");
|
||||
list.append("2");
|
||||
list.append("3");
|
||||
list.append("4");
|
||||
}
|
||||
}
|
||||
|
||||
class LinkedList<T> {
|
||||
private Node<T> head;
|
||||
|
||||
public void append(T value) {
|
||||
if (head == null) {
|
||||
head = new Node<T>(value);
|
||||
} else {
|
||||
Node<T> m = head;
|
||||
while (m.next != null) {
|
||||
m = m.next;
|
||||
}
|
||||
m.next = new Node<T>(value);
|
||||
}
|
||||
}
|
||||
|
||||
public String visualize() {
|
||||
Node<T> list = new Node<T>(null);
|
||||
list.next = this.head;
|
||||
|
||||
return GraphData.from(list, (item, info) -> {
|
||||
if (item != list) {
|
||||
info.id = item.value.toString();
|
||||
} else {
|
||||
info.label = "list";
|
||||
}
|
||||
if (item.next != null) {
|
||||
EdgeInfo<LinkedList.Node<T>> ei = info.addEdge(item.next);
|
||||
ei.label = "next";
|
||||
}
|
||||
return info;
|
||||
}).toString();
|
||||
}
|
||||
|
||||
static class Node<T> {
|
||||
public T value;
|
||||
public Node<T> next;
|
||||
|
||||
public Node(T value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user