fork(1) 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 peter = new Node("Peter");
  17.  
  18. Node francis = new Node("Francis");
  19.  
  20. Node gilbert = new Node("Gilbert");
  21. dbl.setTail(gilbert);
  22.  
  23. jacques.setNextNode(joseph);
  24.  
  25. joseph.setPreviousNode(jacques);
  26. joseph.setNextNode(peter);
  27.  
  28. peter.setPreviousNode(joseph);
  29. peter.setNextNode(francis);
  30.  
  31. francis.setPreviousNode(peter);
  32. francis.setNextNode(gilbert);
  33.  
  34. gilbert.setPreviousNode(francis);
  35.  
  36. System.out.println(dbl.toString());
  37. System.out.println("++++++++++++++++++++++++++++++++");
  38. System.out.println(dbl.reverseToString());
  39. System.out.println("++++++++++++++++++++++++++++++++");
  40. System.out.println("++++++++++++++++++++++++++++++++");
  41. System.out.println("++++++++++++++++++++++++++++++++");
  42. DoubleLinkedList.reverseTwoNode(joseph, francis);
  43. System.out.println(dbl.toString());
  44. System.out.println("++++++++++++++++++++++++++++++++");
  45. System.out.println(dbl.reverseToString());
  46. }
  47.  
  48. }
  49.  
  50. class DoubleLinkedList {
  51.  
  52. private Node head; //the head node pointer of the DLL
  53. private Node tail; //the tail node pointer of the DLL
  54. private int size; //The size of the DLL, number of String
  55.  
  56.  
  57. public static void reverseTwoNode(Node n1, Node n2) {
  58. if (n1 == n2.getPreviousNode() || n2 == n1.getPreviousNode()) {
  59. /* Adjacent nodes */
  60.  
  61. /* Order is relevant */
  62. Node first;
  63. Node second;
  64. if (n1 == n2.getPreviousNode()) {
  65. first = n1;
  66. second = n2;
  67. } else {
  68. first = n2;
  69. second = n1;
  70. }
  71.  
  72. first.setNextNode(second.getNextNode());
  73. second.setPreviousNode(first.getPreviousNode());
  74.  
  75. if (first.getNextNode() != null)
  76. first.getNextNode().setPreviousNode(first);
  77.  
  78. if (second.getPreviousNode() != null)
  79. second.getPreviousNode().setNextNode(second);
  80.  
  81. second.setNextNode(first);
  82. first.setPreviousNode(second);
  83. } else {
  84. /* Non adjacent */
  85. Node prevN1 = n1.getPreviousNode();
  86. Node nextN1 = n1.getNextNode();
  87. Node prevN2 = n2.getPreviousNode();
  88. Node nextN2 = n2.getNextNode();
  89.  
  90. n1.setPreviousNode(prevN2);
  91. n1.setNextNode(nextN2);
  92. n2.setPreviousNode(prevN1);
  93. n2.setNextNode(nextN1);
  94.  
  95. if (prevN1 != null)
  96. prevN1.setNextNode(n2);
  97. if (nextN1 != null)
  98. nextN1.setPreviousNode(n2);
  99. if (prevN2 != null)
  100. prevN2.setNextNode(n1);
  101. if (nextN2 != null)
  102. nextN2.setPreviousNode(n1);
  103. }
  104. }
  105.  
  106. public static void reverseTwoNodeOriginalCode(Node N1, Node N2) {
  107. N1.setNextNode(N2.getNextNode());
  108. N2.setPreviousNode(N1.getPreviousNode());
  109.  
  110. if (N1.getNextNode() != null)
  111. N1.getNextNode().setPreviousNode(N1);
  112.  
  113. if (N2.getPreviousNode() != null)
  114. N2.getPreviousNode().setNextNode(N2);
  115.  
  116. N2.setNextNode(N1);
  117. N1.setPreviousNode(N2);
  118. }
  119.  
  120. public DoubleLinkedList() {
  121. this.size = 0;
  122. this.setTail(null);
  123. this.setHead(null);
  124. }
  125.  
  126. public String toString() {
  127. String str = "List of candidate \n";
  128. str += "head-->";
  129.  
  130. Node iterator = this.getHead();
  131.  
  132. while (iterator != null) {
  133. str += iterator.getString();
  134. str += "-->";
  135. iterator = iterator.getNextNode();
  136. }
  137. return str + "tail";
  138. }
  139.  
  140. /**
  141.   * Return string that display DLL from tail to head
  142.   *
  143.   * @return str
  144.   */
  145. public String reverseToString() {
  146. String str = "Reverse\n";
  147. str += "tail-->";
  148.  
  149. Node iterator = this.getTail();
  150.  
  151. while (iterator != null) {
  152. str += iterator.getString();
  153. str += "-->";
  154. iterator = iterator.getPreviousNode();
  155. }
  156.  
  157. return (str + "head");
  158. }
  159.  
  160. public Node getNode(int index) {
  161.  
  162. Node result = null;
  163. if (index >= 0 && index < this.size) {
  164. result = this.getHead();
  165. for (int i = 0; i < index; i++) {
  166. result = result.getNextNode();
  167. }
  168. }
  169. return result;
  170. }
  171.  
  172. public Node getHead() {
  173. return head;
  174. }
  175.  
  176. public void setHead(Node head) {
  177. this.head = head;
  178. }
  179.  
  180. public Node getTail() {
  181. return tail;
  182. }
  183.  
  184. public void setTail(Node tail) {
  185. this.tail = tail;
  186. }
  187. }
  188.  
  189. class Node {
  190.  
  191. private Node prevNode; //Pointer to the previous Node
  192. private Node nextNode; //Pointer to the next Node
  193. private String candidate; //String that own the Node
  194.  
  195. /**
  196.   * Constructor
  197.   * Simple constructor that fill instantiate candidate provided,
  198.   * and initialize prevNode and nextNode to null
  199.   *
  200.   * @param C
  201.   */
  202. public Node(String C) {
  203. this.prevNode = null;
  204. this.nextNode = null;
  205. this.candidate = C;
  206. }
  207.  
  208. /**
  209.   * Advanced Constructor
  210.   * Identical to the previous Construtor but allow to set a previousNode and nextNode
  211.   *
  212.   * @param C
  213.   * @param prevNode
  214.   * @param nextNode
  215.   */
  216. public Node(String C, Node prevNode, Node nextNode) {
  217. this.prevNode = prevNode;
  218. this.nextNode = nextNode;
  219. this.candidate = C;
  220. }
  221.  
  222. /**
  223.   * Get String from the current node
  224.   *
  225.   * @return candidate
  226.   */
  227. public String getString() {
  228. return this.candidate;
  229. }
  230.  
  231. /**
  232.   * Set the provided candidate to the current Node
  233.   *
  234.   * @param C
  235.   */
  236. public void setString(String C) {
  237. this.candidate = C;
  238. }
  239.  
  240. /**
  241.   * Get the previous Node from the current Node
  242.   *
  243.   * @return prevNode
  244.   */
  245. public Node getPreviousNode() {
  246. return this.prevNode;
  247. }
  248.  
  249. /**
  250.   * Set the previous Node to the current Node
  251.   *
  252.   * @param N
  253.   */
  254. public void setPreviousNode(Node N) {
  255. this.prevNode = N;
  256. }
  257.  
  258. /**
  259.   * Get the next Node from the current Node
  260.   *
  261.   * @return nextNode
  262.   */
  263. public Node getNextNode() {
  264. return this.nextNode;
  265. }
  266.  
  267. /**
  268.   * Set the a provided Node to the nextNode of the current Node
  269.   *
  270.   * @param N
  271.   */
  272. public void setNextNode(Node N) {
  273. this.nextNode = N;
  274. }
  275. }
Success #stdin #stdout 0.11s 320256KB
stdin
Standard input is empty
stdout
List of candidate 
head-->Jacques-->Joseph-->Peter-->Francis-->Gilbert-->tail
++++++++++++++++++++++++++++++++
Reverse
tail-->Gilbert-->Francis-->Peter-->Joseph-->Jacques-->head
++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++
List of candidate 
head-->Jacques-->Francis-->Peter-->Joseph-->Gilbert-->tail
++++++++++++++++++++++++++++++++
Reverse
tail-->Gilbert-->Joseph-->Peter-->Francis-->Jacques-->head