fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. class Ideone {
  8. public static void main(String[] args) {
  9. DoubleLinkedList dbl = new DoubleLinkedList();
  10.  
  11. Node jacques = new Node("Jacques");
  12. dbl.setHead(jacques);
  13.  
  14. Node joseph = new Node("Joseph");
  15.  
  16. Node francis = new Node("Francis");
  17.  
  18. Node gilbert = new Node("Gilbert");
  19. dbl.setTail(gilbert);
  20.  
  21. jacques.setNextNode(joseph);
  22.  
  23. joseph.setPreviousNode(jacques);
  24. joseph.setNextNode(francis);
  25.  
  26. francis.setPreviousNode(joseph);
  27. francis.setNextNode(gilbert);
  28.  
  29. gilbert.setPreviousNode(francis);
  30.  
  31. System.out.println(dbl.toString());
  32. System.out.println("++++++++++++++++++++++++++++++++");
  33. System.out.println(dbl.reverseToString());
  34. System.out.println("++++++++++++++++++++++++++++++++");
  35. System.out.println("++++++++++++++++++++++++++++++++");
  36. System.out.println("++++++++++++++++++++++++++++++++");
  37. DoubleLinkedList.reverseTwoNode(joseph, francis);
  38. System.out.println(dbl.toString());
  39. System.out.println("++++++++++++++++++++++++++++++++");
  40. System.out.println(dbl.reverseToString());
  41. }
  42.  
  43. }
  44.  
  45. class DoubleLinkedList {
  46.  
  47. private Node head; //the head node pointer of the DLL
  48. private Node tail; //the tail node pointer of the DLL
  49. private int size; //The size of the DLL, number of String
  50.  
  51.  
  52.  
  53.  
  54. public static void reverseTwoNode(Node N1, Node N2) {
  55. N1.setNextNode(N2.getNextNode());
  56. N2.setPreviousNode(N1.getPreviousNode());
  57.  
  58. if (N1.getNextNode() != null)
  59. N1.getNextNode().setPreviousNode(N1);
  60.  
  61. if (N2.getPreviousNode() != null)
  62. N2.getPreviousNode().setNextNode(N2);
  63.  
  64. N2.setNextNode(N1);
  65. N1.setPreviousNode(N2);
  66. }
  67.  
  68. public DoubleLinkedList() {
  69. this.size = 0;
  70. this.setTail(null);
  71. this.setHead(null);
  72. }
  73.  
  74. public String toString() {
  75. String str = "List of candidate \n";
  76. str += "head-->";
  77.  
  78. Node iterator = this.getHead();
  79.  
  80. while (iterator != null) {
  81. str += iterator.getString();
  82. str += "-->";
  83. iterator = iterator.getNextNode();
  84. }
  85. return str + "tail";
  86. }
  87.  
  88. /**
  89.   * Return string that display DLL from tail to head
  90.   *
  91.   * @return str
  92.   */
  93. public String reverseToString() {
  94. String str = "Reverse\n";
  95. str += "tail-->";
  96.  
  97. Node iterator = this.getTail();
  98.  
  99. while (iterator != null) {
  100. str += iterator.getString();
  101. str += "-->";
  102. iterator = iterator.getPreviousNode();
  103. }
  104.  
  105. return (str + "head");
  106. }
  107.  
  108. public Node getNode(int index) {
  109.  
  110. Node result = null;
  111. if (index >= 0 && index < this.size) {
  112. result = this.getHead();
  113. for (int i = 0; i < index; i++) {
  114. result = result.getNextNode();
  115. }
  116. }
  117. return result;
  118. }
  119.  
  120. public Node getHead() {
  121. return head;
  122. }
  123.  
  124. public void setHead(Node head) {
  125. this.head = head;
  126. }
  127.  
  128. public Node getTail() {
  129. return tail;
  130. }
  131.  
  132. public void setTail(Node tail) {
  133. this.tail = tail;
  134. }
  135. }
  136.  
  137. class Node {
  138.  
  139. private Node prevNode; //Pointer to the previous Node
  140. private Node nextNode; //Pointer to the next Node
  141. private String candidate; //String that own the Node
  142.  
  143. /**
  144.   * Constructor
  145.   * Simple constructor that fill instantiate candidate provided,
  146.   * and initialize prevNode and nextNode to null
  147.   *
  148.   * @param C
  149.   */
  150. public Node(String C) {
  151. this.prevNode = null;
  152. this.nextNode = null;
  153. this.candidate = C;
  154. }
  155.  
  156. /**
  157.   * Advanced Constructor
  158.   * Identical to the previous Construtor but allow to set a previousNode and nextNode
  159.   *
  160.   * @param C
  161.   * @param prevNode
  162.   * @param nextNode
  163.   */
  164. public Node(String C, Node prevNode, Node nextNode) {
  165. this.prevNode = prevNode;
  166. this.nextNode = nextNode;
  167. this.candidate = C;
  168. }
  169.  
  170. /**
  171.   * Get String from the current node
  172.   *
  173.   * @return candidate
  174.   */
  175. public String getString() {
  176. return this.candidate;
  177. }
  178.  
  179. /**
  180.   * Set the provided candidate to the current Node
  181.   *
  182.   * @param C
  183.   */
  184. public void setString(String C) {
  185. this.candidate = C;
  186. }
  187.  
  188. /**
  189.   * Get the previous Node from the current Node
  190.   *
  191.   * @return prevNode
  192.   */
  193. public Node getPreviousNode() {
  194. return this.prevNode;
  195. }
  196.  
  197. /**
  198.   * Set the previous Node to the current Node
  199.   *
  200.   * @param N
  201.   */
  202. public void setPreviousNode(Node N) {
  203. this.prevNode = N;
  204. }
  205.  
  206. /**
  207.   * Get the next Node from the current Node
  208.   *
  209.   * @return nextNode
  210.   */
  211. public Node getNextNode() {
  212. return this.nextNode;
  213. }
  214.  
  215. /**
  216.   * Set the a provided Node to the nextNode of the current Node
  217.   *
  218.   * @param N
  219.   */
  220. public void setNextNode(Node N) {
  221. this.nextNode = N;
  222. }
  223. }
Success #stdin #stdout 0.1s 320256KB
stdin
Standard input is empty
stdout
List of candidate 
head-->Jacques-->Joseph-->Francis-->Gilbert-->tail
++++++++++++++++++++++++++++++++
Reverse
tail-->Gilbert-->Francis-->Joseph-->Jacques-->head
++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++
List of candidate 
head-->Jacques-->Francis-->Joseph-->Gilbert-->tail
++++++++++++++++++++++++++++++++
Reverse
tail-->Gilbert-->Joseph-->Francis-->Jacques-->head