Advertisement
kerelius

Untitled

Sep 25th, 2019
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3. Online Java Compiler.
  4. Code, Compile, Run and Debug java program online.
  5. Write your code in this editor and press "Run" button to execute it.
  6.  
  7. *******************************************************************************/
  8.  
  9. public class Main
  10. {
  11. public static void main(String[] args) {
  12.  
  13. LinkedTwoEndedList LinckedList = new LinkedTwoEndedList();
  14. }
  15. }
  16.  
  17. /**
  18. * A list that contains objects that can be inserted
  19. * or removed from either end of the list
  20. * as desired (but not the middle).
  21. */
  22.  
  23. public class LinkedTwoEndedList implements TwoEndedList {
  24.  
  25. private ListNode front;
  26.  
  27. private int number = 0;
  28.  
  29. // Instance variables representing ADT
  30. //
  31. // - temp0 is the new node to be added to the front
  32. // - temp1 is the new node to be added to the back
  33. // - temp2 is a temporary node
  34. // - temp3 finds the last node
  35. // - temp4 is the node to be deleted from the front
  36. // - temp5 finds the last node
  37. // - temp6 is the second-to-last node
  38.  
  39.  
  40. public void addFront (Object o) {
  41.  
  42. ListNode temp0;
  43. if (size() < 1) {
  44. front = new ListNode(o);
  45. number++;
  46.  
  47. }
  48.  
  49. else {
  50.  
  51. // hook it up
  52. temp0 = front;
  53. front = new ListNode(o);
  54. front.next = temp0;
  55. temp0.previous = front;
  56. number++; }
  57.  
  58. }
  59.  
  60.  
  61. public void addBack (Object o) {
  62. ListNode temp1 = new ListNode (o);
  63. ListNode temp2 = front;
  64. if (front != null) {
  65. while (temp2.next != null) {
  66. temp2 = temp2.next;
  67. }
  68. temp2.next = temp1;
  69. temp1.previous = temp2
  70. } else {
  71. front = temp1;
  72. }
  73. // increase the size
  74. number++;
  75. }
  76.  
  77. public Object getFirst () {
  78. return front.contents;
  79. }
  80.  
  81. public Object getLast () {
  82.  
  83. // Initialize temp3 to a new node.
  84. ListNode temp3 = new ListNode(null);
  85.  
  86. // Set temp3 to the front.
  87. temp3 = front;
  88.  
  89. // As long as temp3's next isn't null...
  90. while (temp3.next != null) {
  91. // move to the next node.
  92. temp3 = temp3.next;
  93. }
  94.  
  95. // Finally, return temp3's contents.
  96. return temp3.contents;
  97. }
  98.  
  99. public Object removeFirst () {
  100. ListNode temp4 = new ListNode(null);
  101. if (front!=null && front.next!=null) {
  102. // the list has something in it
  103. temp4 = front;
  104. front = front.next;
  105. front.next.previous = null;
  106. temp4.next = null;
  107. }
  108. else if (front!=null && front.next==null) {
  109. // the list has something in it
  110. temp4 = front;
  111. front = null;
  112. }
  113. else {
  114. // the list has nothing in it
  115. temp4 = null;
  116. }
  117. return temp4;
  118. }
  119.  
  120. public Object removeLast () {
  121. // start at the front
  122. ListNode temp5 = front;
  123. ListNode temp6 = front;
  124. if (front != null) {
  125. // find the end
  126. while (temp5.next != null) {
  127. temp6 = temp5;
  128. temp5 = temp5.next;
  129. }
  130. temp6.next = null;
  131. }
  132. // decrease it
  133. number--;
  134. return temp5.contents;
  135. }
  136.  
  137. public int size () {
  138. return number;
  139. }
  140. }
  141.  
  142. /**
  143. * A two-ended list. This is a hybrid of a stack and a queue; it contains
  144. * objects, and one can insert or remove from either end as desired (but not
  145. * the middle).
  146. *
  147. * You must not modify this interface.
  148. */
  149.  
  150. public interface TwoEndedList {
  151.  
  152. /**
  153. * Add o at the front of my list.
  154. *
  155. * @param Object o added to the front of my list.
  156. */
  157.  
  158. public void addFront(Object o);
  159.  
  160. /**
  161. * Add o at the back of my list.
  162. *
  163. * @param Object o added to the back of my list.
  164. */
  165.  
  166. public void addBack(Object o);
  167.  
  168. /**
  169. * Return the first object in my list. If I am empty,
  170. * just return null.
  171. *
  172. * @return Object the first object in my list, null if I am empty.
  173. */
  174.  
  175. public Object getFirst();
  176.  
  177. /**
  178. * Return the last object in my list. If I am empty,
  179. * just return null.
  180. *
  181. * @return Object the last object in my list, null if I am empty.
  182. */
  183.  
  184. public Object getLast();
  185.  
  186. /**
  187. * Remove and return the first object in my list. If I am empty,
  188. * just return null.
  189. *
  190. * @return Object the first object in my list, null if I am empty.
  191. */
  192.  
  193. public Object removeFirst();
  194.  
  195. /**
  196. * Remove and return the last object in my list. If I am empty,
  197. * just return null.
  198. *
  199. * @return Object the last object in my list, null if I am empty.
  200. */
  201.  
  202. public Object removeLast();
  203.  
  204. /**
  205. * Return the number of objects in my list.
  206. *
  207. * @return int the number of objects in my list.
  208. */
  209.  
  210. public int size();
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement