Guest User

Untitled

a guest
Jan 12th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.72 KB | None | 0 0
  1. /* DList.java */
  2.  
  3. package list;
  4.  
  5. /**
  6. * A DList is a mutable doubly-linked list ADT. Its implementation is
  7. * circularly-linked and employs a sentinel (dummy) node at the head
  8. * of the list.
  9. *
  10. * DO NOT CHANGE ANY METHOD PROTOTYPES IN THIS FILE.
  11. */
  12.  
  13. public class DList {
  14.  
  15. /**
  16. * head references the sentinel node.
  17. * size is the number of items in the list. (The sentinel node does not
  18. * store an item.)
  19. *
  20. * DO NOT CHANGE THE FOLLOWING FIELD DECLARATIONS.
  21. */
  22.  
  23. protected DListNode head;
  24. protected int size; // the defaut value is 0
  25.  
  26. /* DList invariants:
  27. * 1) head != null.
  28. * 2) For any DListNode x in a DList, x.next != null.
  29. * 3) For any DListNode x in a DList, x.prev != null.
  30. * 4) For any DListNode x in a DList, if x.next == y, then y.prev == x.
  31. * 5) For any DListNode x in a DList, if x.prev == y, then y.next == x.
  32. * 6) size is the number of DListNodes, NOT COUNTING the sentinel,
  33. * that can be accessed from the sentinel (head) by a sequence of
  34. * "next" references.
  35. */
  36.  
  37. /**
  38. * newNode() calls the DListNode constructor. Use this class to allocate
  39. * new DListNodes rather than calling the DListNode constructor directly.
  40. * That way, only this method needs to be overridden if a subclass of DList
  41. * wants to use a different kind of node.
  42. * @param item the item to store in the node.
  43. * @param prev the node previous to this node.
  44. * @param next the node following this node.
  45. */
  46. protected DListNode newNode(Object item, DListNode prev, DListNode next) {
  47. return new DListNode(item, prev, next);
  48. }
  49.  
  50. /**
  51. * DList() constructor for an empty DList.
  52. */
  53.  
  54. // if(d.head != null && x.next != null && x.prev != null && )
  55. // We need a sentinel node to "next" to the first node in the list;
  56. // but the sentinal node is not included in the List size
  57.  
  58. /* A basic doubly linked list implementation */
  59. // public class DoublyLinkedList<E>{
  60. // // nested Node class//
  61. //
  62. // // already included in the DListNode file//
  63. //
  64. // }
  65. public DList() { // construct a new empty list //
  66. head = new DListNode(null, null, null);
  67. head.setNext(head);
  68. head.setPrev(head);
  69. // Your solution here.
  70. }
  71.  
  72. /**
  73. * isEmpty() returns true if this DList is empty, false otherwise.
  74. * @return true if this DList is empty, false otherwise.
  75. * Performance: runs in O(1) time.
  76. */
  77. public boolean isEmpty() {
  78. return size == 0;
  79. }
  80.  
  81. /**
  82. * length() returns the length of this DList.
  83. * @return the length of this DList.
  84. * Performance: runs in O(1) time.
  85. */
  86. public int length() {
  87. return size;
  88. }
  89.  
  90. /**
  91. * insertFront() inserts an item at the front of this DList.
  92. * @param item is the item to be inserted.
  93. * Performance: runs in O(1) time.
  94. */
  95. public void insertFront(Object item) {
  96. DListNode newest = newNode(item, head, head.next);
  97. head.next.setPrev(newest);
  98. head.setNext(newest);
  99. size ++;
  100. // Your solution here.
  101. }
  102.  
  103. /**
  104. * insertBack() inserts an item at the back of this DList.
  105. * @param item is the item to be inserted.
  106. * Performance: runs in O(1) time.
  107. */
  108. public void insertBack(Object item) {
  109. DListNode newest = newNode(item, head.prev, head);
  110. head.prev.setNext(newest);
  111. head.setPrev(newest);
  112. size ++;
  113. // Your solution here.
  114. }
  115.  
  116. /**
  117. * front() returns the node at the front of this DList. If the DList is
  118. * empty, return null.
  119. *
  120. * Do NOT return the sentinel under any circumstances!
  121. *
  122. * @return the node at the front of this DList.
  123. * Performance: runs in O(1) time.
  124. */
  125. public DListNode front() { // addFirst
  126. if(size != 0){
  127. return head.next;
  128. }else{return null;}
  129.  
  130.  
  131. // Your solution here.
  132. }
  133.  
  134. /**
  135. * back() returns the node at the back of this DList. If the DList is
  136. * empty, return null.
  137. *
  138. * Do NOT return the sentinel under any circumstances!
  139. *
  140. * @return the node at the back of this DList.
  141. * Performance: runs in O(1) time.
  142. */
  143. public DListNode back() { // addLast
  144. return head.prev;
  145. // Your solution here.
  146. }
  147.  
  148. /**
  149. * next() returns the node following "node" in this DList. If "node" is
  150. * null, or "node" is the last node in this DList, return null.
  151. *
  152. * Do NOT return the sentinel under any circumstances!
  153. *
  154. * @param node the node whose successor is sought.
  155. * @return the node following "node".
  156. * Performance: runs in O(1) time.
  157. */
  158. public DListNode next(DListNode node) {
  159. if( node.item == null || node.next == head){
  160. return null;
  161. }else{
  162. return node.next;
  163. }
  164. // Your solution here.
  165. }
  166.  
  167. /**
  168. * prev() returns the node prior to "node" in this DList. If "node" is
  169. * null, or "node" is the first node in this DList, return null.
  170. *
  171. * Do NOT return the sentinel under any circumstances!
  172. *
  173. * @param node the node whose predecessor is sought.
  174. * @return the node prior to "node".
  175. * Performance: runs in O(1) time.
  176. */
  177. public DListNode prev(DListNode node) {
  178. if( node.item == null || node.prev == head ){
  179. return null;
  180. }else{
  181. return node.prev;
  182. }
  183. // Your solution here.
  184. }
  185.  
  186. /**
  187. * insertAfter() inserts an item in this DList immediately following "node".
  188. * If "node" is null, do nothing.
  189. * @param item the item to be inserted.
  190. * @param node the node to insert the item after.
  191. * Performance: runs in O(1) time.
  192. */
  193. public void insertAfter(Object item, DListNode node) {
  194. if(node.item != null){
  195. DListNode insAfter_Node = newNode(item, node, node.next);
  196. node.next.prev = insAfter_Node;
  197. node.next = insAfter_Node;
  198. size ++;
  199. }
  200. // Your solution here.
  201. }
  202.  
  203. /**
  204. * insertBefore() inserts an item in this DList immediately before "node".
  205. * If "node" is null, do nothing.
  206. * @param item the item to be inserted.
  207. * @param node the node to insert the item before.
  208. * Performance: runs in O(1) time.
  209. */
  210. public void insertBefore(Object item, DListNode node) {
  211. if(node.item != null){
  212. DListNode insBefore_Node = newNode(item, node.prev, node);
  213. node.prev.next = insBefore_Node;
  214. node.prev = insBefore_Node;
  215. size ++;
  216. }
  217. // Your solution here.
  218. }
  219.  
  220. /**
  221. * remove() removes "node" from this DList. If "node" is null, do nothing.
  222. * Performance: runs in O(1) time.
  223. */
  224. public void remove(DListNode node) {
  225. node.prev.next = node.next;
  226. node.next.prev = node.prev;
  227. size --;
  228. // Your solution here.
  229. }
  230.  
  231. /**
  232. * toString() returns a String representation of this DList.
  233. *
  234. * DO NOT CHANGE THIS METHOD.
  235. *
  236. * @return a String representation of this DList.
  237. * Performance: runs in O(n) time, where n is the length of the list.
  238. */
  239. public String toString() {
  240. String result = "[ ";
  241. DListNode current = head.next;
  242. while (current != head) {
  243. result = result + current.item + " ";
  244. current = current.next;
  245. }
  246. return result + "]";
  247. }
  248.  
  249. /* Adds element e to the linked list in between the given nodes. */
  250. public void addBetween(Object e, DListNode predecessor, DListNode successor){
  251. // create and link a new node
  252. DListNode newest = newNode(e, predecessor, successor);
  253. predecessor.setNext(newest);
  254. successor.setPrev(newest);
  255. size ++;
  256. }
  257.  
  258. public static void main (String[] args) {
  259. DList AList = new DList(); // construct a new empty list //
  260. AList.insertFront("1");
  261. AList.insertBack("4");
  262. AList.insertAfter("2", AList.head.next);
  263. AList.insertBefore("3", AList.head.prev);
  264.  
  265. System.out.println(AList);
  266. System.out.println(AList.front().item);
  267. System.out.println(AList.back().item);
  268.  
  269. }
  270. }
Add Comment
Please, Sign In to add comment