Advertisement
Guest User

DLL

a guest
Jul 11th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.88 KB | None | 0 0
  1. //This will implement a "doubly linked list" that is set up to hold whatever
  2. //the user defines it to hold at run time (using Generics)
  3.  
  4. import java.util.*;
  5.  
  6. public class DLList<E>
  7. {
  8. //data
  9. protected DLLNode<E> head, tail;
  10.  
  11. //constructor
  12. public DLList()
  13. {
  14. head = tail = null;
  15. }
  16.  
  17. //methods
  18. //-----------------------------------
  19. //addLast - adds theElement to the end of the list in its own node
  20. public void addLast(E theElement)
  21. {
  22. //create a node to hold it
  23. DLLNode<E> newNode = new DLLNode<E>(theElement);
  24.  
  25. //if the list is empty, then it becomes the only node
  26. if (head == null)
  27. head = tail = newNode;
  28.  
  29. //otherwise, change the links so it is the last node
  30. else
  31. {
  32. tail.next = newNode;
  33. newNode.prev = tail;
  34. tail = newNode;
  35. }
  36. }
  37.  
  38. //-----------------------------------
  39. //addFirst - adds theElement to the front of the list in its own node
  40. public void addFirst(E theElement)
  41. {
  42. //create a node to hold it
  43. DLLNode<E> newNode = new DLLNode<E>(theElement);
  44.  
  45. //if the list is empty, then it becomes the only node
  46. if (head == null)
  47. head = tail = newNode;
  48.  
  49. //otherwise, change the links so it is the first node
  50. else
  51. {
  52. newNode.next = head;
  53. head.prev = newNode;
  54. head = newNode;
  55. }
  56. }
  57.  
  58. //-----------------------------------
  59. //toString - returns its representation as a String
  60. public String toString()
  61. {
  62. String outString = "[";
  63. DLLNode<E> ptr = head;
  64. while (ptr != null)
  65. {
  66. if (ptr != head) //only put in , if not the head
  67. outString = outString + ", " + ptr;
  68. else
  69. outString = outString + " " + ptr; //just a blank, then data pointed to by ptr
  70. ptr = ptr.next;
  71. }
  72. outString = outString + " ]";
  73.  
  74. return outString;
  75. }
  76.  
  77. //backwards - returns its backwards representation as a String by following the prev links
  78. public String backwards()
  79. {
  80. String outString = "[";
  81. DLLNode<E> ptr = tail;
  82. while (ptr != null)
  83. {
  84. if (ptr != tail) //only put in , if not the tail
  85. outString = outString + ", " + ptr;
  86. else
  87. outString = outString + " " + ptr; //just a blank, then data pointed to by ptr
  88. ptr = ptr.prev;
  89. }
  90. outString = outString + " ]";
  91.  
  92. return outString;
  93. }
  94.  
  95. //-----------------------------------
  96. //size - traverses the list, counting how many elements there are. Returns size
  97. public int size()
  98. {
  99. int total = 0;
  100. DLLNode<E> ptr = head;
  101. while (ptr != null) //walks through the linked list,
  102. {
  103. total++; //counting up how many nodes there are
  104. ptr = ptr.next;
  105. }
  106. return total;
  107. }
  108.  
  109. //-----------------------------------
  110. //isEmpty - returns if it is empty
  111. public boolean isEmpty()
  112. {
  113. return head == null;
  114. }
  115.  
  116. //-----------------------------------
  117. //clear - "clears" the list by resetting head and tail to null
  118. public void clear()
  119. {
  120. head = tail = null;
  121. }
  122.  
  123. //-----------------------------------
  124. //getFirst - returns the first element on the list without removing it
  125. public E getFirst()
  126. {
  127. if (head == null) //empty
  128. throw new NoSuchElementException("the list is empty");
  129. return head.data;
  130. }
  131.  
  132. //-----------------------------------
  133. //getLast - returns the last element on the list without removing it
  134. public E getLast()
  135. {
  136. if (tail == null) //empty
  137. throw new NoSuchElementException("the list is empty");
  138. return tail.data;
  139. }
  140.  
  141. //-----------------------------------
  142. //removeFirst - removes and returns the first element on the list
  143. public E removeFirst()
  144. {
  145. //case 1: is it empty?
  146. if (head == null)
  147. throw new NoSuchElementException("the list is empty");
  148.  
  149. //case 2: if there is only 1 element on the list
  150. else if (head == tail) //already handled the case where they were both null
  151. {
  152. E dataToReturn = head.data; //store the data that will be returned
  153. head = tail = null; //make the list empty
  154. return dataToReturn; //return the data
  155. }
  156.  
  157. //case 3: if there are lots of elements on the list
  158. else
  159. {
  160. E dataToReturn = head.data; //store the data that will be returned
  161. head = head.next; //move head over
  162. head.prev = null; //take out the prev link from the first node
  163.  
  164. return dataToReturn;
  165. }
  166. }
  167.  
  168. //-----------------------------------
  169. //removeLast - removes and returns the last element on the list
  170. public E removeLast()
  171. {
  172. //case 1: is it empty?
  173. if (head == null)
  174. throw new NoSuchElementException("the list is empty");
  175.  
  176. //case 2: if there is only 1 element on the list
  177. else if (head == tail) //already handled the case where they were both null
  178. {
  179. E dataToReturn = tail.data; //store the data that will be returned
  180. head = tail = null; //make the list empty
  181. return dataToReturn; //return the data
  182. }
  183.  
  184. //case 3: if there are lots of elements on the list
  185. else
  186. {
  187. E dataToReturn = tail.data; //store the data that will be returned
  188. tail = tail.prev;
  189. tail.next = null; //last element no longer points at anything
  190.  
  191. return dataToReturn;
  192. }
  193. }
  194.  
  195. //-----------------------------------
  196. //remove - removes the first instance of the "doomed" element
  197. public boolean remove(E doomed)
  198. {
  199. //case 1: list is empty
  200. if (head == null)
  201. return false; //means it did not find it
  202.  
  203. //case2: list only has 1 element
  204. else if (head == tail)
  205. if (head.data.equals(doomed))
  206. {
  207. removeFirst();
  208. return true;
  209. }
  210. else
  211. return false;
  212.  
  213. //case3: doomed is at the front of the list
  214. else if (head.data.equals(doomed))
  215. { removeFirst(); //don't do anything with what it returned
  216. return true;
  217. }
  218.  
  219. //case4: doomed is at the end of the list
  220. else if (tail.data.equals(doomed))
  221. { removeLast(); //don't do anything with what it returned
  222. return true;
  223. }
  224.  
  225. //case5: doomed is in the middle of the list somewhere
  226. else
  227. {
  228. DLLNode<E> ptr = head;
  229. while (ptr != null && !ptr.data.equals(doomed))
  230. {
  231. ptr = ptr.next;
  232. }
  233.  
  234. //should stop at the node
  235. if (ptr == null) //if if did not find it
  236. return false;
  237. else //found it!
  238. {
  239. ptr.prev.next = ptr.next; //move the next link so it goes around the doomed node
  240. ptr.next.prev = ptr.prev; //move the prev link so it goes around the doomed node
  241. return true;
  242. }
  243. }
  244. }
  245.  
  246. //-----------------------------------
  247. //recursiveToString() - "starting" method for recursively traversing a list and returning its contents
  248. // it just calls the recursive version.
  249. public String recursiveToString()
  250. {
  251. return recursiveToString2(head);
  252. }
  253.  
  254. //-----------------------------------
  255. //recursiveToString2 - recursively traverses whatever list is is passed by calling itself
  256. private String recursiveToString2(DLLNode start)
  257. {
  258. if (start == null)
  259. return "";
  260. else
  261. {
  262. //this way returns the contents backwards
  263. return recursiveToString2(start.next) + " " + start.data; //backwards - recursion then data
  264. //return start.data + " " + recursiveToString2(start.next); //forwards - data then recursion
  265. }
  266. }
  267.  
  268.  
  269.  
  270. }
  271.  
  272. //-----------------------------------
  273. //This is the DLLNode class which will implement the nodes that make up an DLList
  274. class DLLNode<E>
  275. {
  276. //data
  277. public E data;
  278. public DLLNode<E> next;
  279. public DLLNode<E> prev;
  280.  
  281. //constructor
  282. public DLLNode(E theData)
  283. {
  284. data = theData;
  285. next = prev = null;
  286. }
  287.  
  288. //methods
  289. //toString - returns its representation as a String
  290. public String toString()
  291. {
  292. return "" + data;
  293. }
  294.  
  295.  
  296.  
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement