Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. class Solution {
  2. public static class Node {
  3. // Each node object has these three variables
  4. private Object element;
  5. private Node next;
  6. private Node previous;
  7.  
  8. // Constructor: Creates a Node object with element = e, next = n and previous = p
  9. Node(Object e, Node n, Node p) {
  10. element = e;
  11. next = n;
  12. previous = p;
  13. }
  14.  
  15. // This function gets Object e as input and sets e as the element of the Node
  16. public void setElement(Object e) {
  17. element = e;
  18. }
  19.  
  20. // This function returns the element variable of the Node
  21. public Object getElement() {
  22. return element;
  23. }
  24.  
  25. // This function gets Node n as input and sets the next variable of the current Node object as n.
  26. public void setNext(Node n) {
  27. next = n;
  28. }
  29.  
  30. // This function returns the next Node
  31. public Node getNext() {
  32. return next;
  33. }
  34.  
  35. // This function gets Node n as input and sets the previous variable of the current Node object as p.
  36. public void setPrevious(Node p) {
  37. previous = p;
  38. }
  39.  
  40. // This function returns the previous Node
  41. public Node getPrevious() {
  42. return previous;
  43. }
  44.  
  45. }
  46.  
  47. public static class DLList {
  48.  
  49. // Each object in DLList has one variable head, which points to the starting Node of DLList.
  50. private Node head;
  51. private Node tail;
  52.  
  53. // Implemens an empty constructor which initialises the head variable as null
  54. DLList() {
  55. head = null;
  56. tail = null;
  57. }
  58.  
  59. // Returns the head node of the DLL
  60. public Node getHead() {
  61. return head;
  62. }
  63.  
  64.  
  65.  
  66. // Returns the tail node of the DLL
  67. public Node getTail() {
  68. return tail;
  69. }
  70.  
  71. // Following methods are the ones you are expected to implement
  72.  
  73. // Adds node n to the head of the list.
  74. public void addFirst(Node n) {
  75. if(head == null) {
  76. n.setNext(null);
  77. n.setPrevious(null);
  78.  
  79. head = n;
  80. tail = n;
  81. } else {
  82. n.setNext(head);
  83. n.setPrevious(null);
  84. head.setPrevious(n);
  85. head = n;
  86. }
  87. }
  88.  
  89. //Removes and returns the first node in the list. If the list is empty, returns null.
  90. public Node removeFirst() {
  91. Node temp = null;
  92.  
  93. if(head == null) {
  94. return temp;
  95. }
  96.  
  97. if(size() == 1) {
  98. temp = head;
  99. head = null;
  100. tail = null;
  101. return temp;
  102. }
  103.  
  104. temp = head;
  105. head = head.getNext();
  106. head.setPrevious(null);
  107.  
  108. return temp;
  109.  
  110. }
  111.  
  112. // Adds node n to the end of the list
  113. public void addLast(Node n) {
  114. if(tail != null) {
  115. n.setPrevious(tail);
  116. n.setNext(null);
  117. tail.setNext(n);
  118. tail = n;
  119. } else {
  120. n.setNext(null);
  121. n.setPrevious(null);
  122.  
  123. tail = n;
  124. head = n;
  125. }
  126.  
  127.  
  128. }
  129.  
  130. //Removes and returns the last node in the list.If the list is empty, returns null.
  131. public Node removeLast() {
  132. Node temp = null;
  133.  
  134. if(head == null) {
  135. return temp;
  136. }
  137.  
  138. if(size() == 0) {
  139. temp = head;
  140. tail = null;
  141. head = null;
  142. return temp;
  143. }
  144.  
  145. temp = tail;
  146. tail = tail.getPrevious();
  147. //tail.setPrevious(tail.getPrevious());
  148. tail.setNext(null);
  149.  
  150. return temp;
  151.  
  152. }
  153.  
  154. // Returns the number of nodes in the list
  155. public int size() {
  156. int counter = 1;
  157.  
  158. if(head == null) {
  159. return 0;
  160. }
  161.  
  162. for(Node n = head; n.getNext() != null; n = n.getNext()) {
  163. counter++;
  164. }
  165.  
  166. return counter;
  167. }
  168.  
  169. // Adds node n after the node in position pos. The list is zero indexed, so the first element in the list correspond
  170. // to position 0. If there is no node in position pos, this method adds it to the last position.
  171. public void addAtPosition(Node n, int pos) {
  172. Node temp = head;
  173.  
  174. if(size()-1 <= pos) {
  175. addLast(n);
  176. return;
  177. }
  178.  
  179. for(int i = 0; i < pos; i++) {
  180. temp = temp.getNext();
  181. }
  182.  
  183. n.setPrevious(temp);
  184. n.setNext(temp.getNext());
  185.  
  186. temp.getNext().setPrevious(n);
  187. temp.setNext(n);
  188. }
  189.  
  190. // Remove and return node n at position pos. The list is zero indexed, so the first element in the list correspond
  191. // to position 0. If there is no node in position pos, this method returns null.
  192. public Node removeFromPosition(int pos) {
  193.  
  194. Node temp = this.getHead();
  195.  
  196. //if the list is empty
  197. if (temp == null) {
  198. return null;
  199. }
  200.  
  201. //if the pos is not in the list
  202. if(size() <= pos ) {
  203. return null;
  204. }
  205.  
  206. //if pos is head
  207. if(pos == 0) {
  208. removeFirst();
  209. return temp;
  210. }
  211.  
  212. //if pos is tail
  213. if(temp.getNext() == null) {
  214. temp = this.getTail();
  215. removeLast();
  216. return temp;
  217. }
  218.  
  219. //find the node to be removed
  220. for(int i=0; i < pos; i++) {
  221. temp = temp.getNext();
  222. }
  223.  
  224. temp.getPrevious().setNext(temp.getNext());
  225. temp.getNext().setPrevious(temp.getPrevious());
  226.  
  227. return temp;
  228. }
  229.  
  230. // Returns a new DLL that contains the elements of the current one in reversed order
  231. public DLList reverse() {
  232. DLList reversed = new DLList();
  233. Node next = this.getTail();
  234. while(next!=null){
  235. Node temp = new Node(next.getElement(),null,null);
  236. //System.out.println("next" + next.getElement());
  237. reversed.addLast(temp);
  238. next = next.getPrevious();
  239. }
  240. return reversed;
  241. }
  242.  
  243. }
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement