fork download
  1. class Ideone {
  2. private Node head, tail;
  3.  
  4. public static void main (String[] args) {
  5. Ideone ideone = new Ideone();
  6.  
  7. ideone.add("HOLA");
  8. ideone.add("MUNDO");
  9.  
  10. System.out.println(ideone);
  11. }
  12.  
  13. public void add(String element) {
  14. Node node = new Node(element);
  15.  
  16. if (tail == null) {
  17. head = tail = node;
  18. } else {
  19. head.previous = node;
  20. node.next = head;
  21. head = node;
  22. }
  23. }
  24.  
  25. public String toString() {
  26. StringBuilder builder = new StringBuilder();
  27.  
  28. Node current = head;
  29. while(current != null) {
  30. builder.append(current.element);
  31. current = current.next;
  32. }
  33.  
  34. return builder.toString();
  35. }
  36.  
  37. private static class Node {
  38. String element;
  39. Node previous, next;
  40.  
  41. public Node(String element) {
  42. this.element = element;
  43. }
  44. }
  45. }
Success #stdin #stdout 0.06s 380160KB
stdin
Standard input is empty
stdout
MUNDOHOLA