Advertisement
Guest User

Untitled

a guest
Oct 20th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 KB | None | 0 0
  1. /**
  2. * Amanda Alvarez
  3. *
  4. * Linked List class that implements DataCollection. It also contains a node class
  5. * within this class since it will be used to keep track of items in the list.
  6. */
  7. public class LinkedListDataCollection implements DataCollection {
  8. /**
  9. * Creates the selected node and sets it to null.
  10. */
  11. private Node selected = null;
  12.  
  13. /**
  14. * Creates a variable to be the head of the node in the linked list.
  15. */
  16. private Node head;
  17. /**
  18. * Creates the x and y locations for the item.
  19. */
  20. private int x, y;
  21. /**
  22. * Creates the size of the linked list.
  23. */
  24. private int size;
  25.  
  26. /**
  27. * Default constructor
  28. */
  29. public LinkedListDataCollection() {
  30. }
  31.  
  32.  
  33. /**
  34. * Constructor that takes in an x and y location for an Item.
  35. */
  36. public LinkedListDataCollection(int x, int y) {
  37. this.x = x;
  38. this.y = y;
  39. }
  40.  
  41.  
  42. /**
  43. * Takes in an item and adds it to the list.
  44. */
  45. public void add(Item someItem) {
  46. // Creates a temporary node.
  47. Node tempNode = new Node(someItem);
  48.  
  49. // Sets the head node to the temporary node if it is not null.
  50. if (head == null)
  51. head = tempNode;
  52.  
  53. // Adds an item to the list.
  54. else {
  55. tempNode.setItem(someItem);
  56. tempNode.setNext(head);
  57. head = tempNode;
  58. }
  59.  
  60. // Sets the location of item
  61. someItem.setLocation(x + size * Item.OVERALL_WIDTH, y);
  62.  
  63. }
  64.  
  65. /**
  66. * Removes item from the list.
  67. */
  68. public void remove(){
  69. // If the selected node is not null
  70. if(selected!=null) {
  71. // Create 2 temporary nodes to keep track of where the nodes are.
  72. Node previous = null;
  73. Node current = head;
  74.  
  75. // While the current node is not selected
  76. while (current != selected) {
  77. previous = current; // Sets the previous node to the current node.
  78. current = current.getNext(); // Gets the next node to get the new current node.
  79. }
  80.  
  81. // If the previous temporary node is selected
  82. if (previous != null)
  83. previous.setNext(current.getNext());
  84. else
  85. head = head.getNext();
  86. }
  87.  
  88. // Makes the selected node null.
  89. selected = null;
  90.  
  91. }
  92.  
  93. /**
  94. * This is what changes the node to be highlighted if it equals the item.
  95. */
  96. private void changeSelected(Item someItem) {
  97. // Goes through the list of nodes
  98. for (Node node = head; node.getNext() != null; node = node.getNext()) {
  99. // Highlights the node if it is equal to the item.
  100. if (node.getItem() == someItem) {
  101. (node.getItem()).highlight(true);
  102. }
  103. // Does not highlight the node.
  104. else
  105. (node.getItem()).highlight(false);
  106. }
  107.  
  108. }
  109.  
  110. /**
  111. * The selected is reset to the head of the linked list.
  112. */
  113. public void reset() {
  114. selected = head;
  115. }
  116.  
  117.  
  118. /**
  119. * Takes in an item and resets the selected to be that item.
  120. */
  121. public void reset(Item someItem) {
  122. // Creates a temporary node that will take the place of the head node in the linked list.
  123. Node tempNode = head;
  124.  
  125. // Checks to see if the passed in item is not null.
  126. if (someItem != null) {
  127.  
  128. // Loops through the list while the temporary node is not null
  129. // and the item in the current node is not the item being passed.
  130. while (tempNode.getItem() != someItem && tempNode != null ) {
  131. // Sets the temporary node to the next node in the list
  132. tempNode = tempNode.getNext();
  133. }
  134. // Sets the selected node to temporary node.
  135. selected = tempNode;
  136. changeSelected(selected.getItem());
  137.  
  138. }
  139. // Sets selected to null
  140. else
  141. selected = null;
  142.  
  143. }
  144.  
  145.  
  146. /**
  147. * Checks to see if there is another item in the list and returns it.
  148. */
  149. public Item next() {
  150. Item item;
  151.  
  152. if (head != null) {
  153. item = selected.getItem();
  154. selected = selected.getNext();
  155. }
  156. else
  157. item = null;
  158.  
  159. return item;
  160. }
  161.  
  162.  
  163. /**
  164. * Checks to see if the list has a next item. If
  165. * the head is not null and the selected is not null,
  166. * it returns true.
  167. */
  168. public boolean hasNext() {
  169.  
  170. return (selected != null) && (head != null) ;
  171. }
  172.  
  173.  
  174. /**
  175. * Paints each individual item in the list.
  176. */
  177. public void paint(Graphics pane) {
  178. int items = 0; // Used to move the items over.
  179. Node temp = head; // Temporary node.
  180.  
  181. // Checks to see if the head of the list is not not.
  182. if (head != null)
  183. {
  184. // While the temporary node is not null.
  185. while(temp != null)
  186. {
  187. // Moves items over continuously from the list.
  188. int move = x + items * Item.OVERALL_WIDTH;
  189. temp.getItem().setX(move);
  190. temp.getItem().paint(pane);
  191. temp = temp.getNext();
  192. items++;
  193. }
  194. }
  195.  
  196. }
  197.  
  198. /**
  199. * Inner class that creates the nodes.
  200. */
  201. private class Node {
  202.  
  203. private Item item; // Data that is carried by this node.
  204. private Node next; // Reference to the next node in the list.
  205.  
  206. /**
  207. * Constructor that sets both the item and next Objects as null and takes in some
  208. * item and sets it to the item that is being used in this class.
  209. */
  210. public Node(Item someItem){
  211. item = null;
  212. next = null;
  213. item = someItem;
  214. }
  215.  
  216. /**
  217. * Gets the next node in the list.
  218. */
  219. public Node getNext(){
  220. return next;
  221. }
  222.  
  223. /**
  224. * Sets the next node to some node passed to it.
  225. */
  226. public void setNext(Node someNode){
  227.  
  228. next = someNode;
  229. }
  230.  
  231. /**
  232. * Gets the next item in the list.
  233. */
  234. public Item getItem()
  235. {
  236. return item;
  237. }
  238.  
  239. /**
  240. * Sets the next item to the item passed to it.
  241. */
  242. public void setItem(Item someItem)
  243. {
  244. item = someItem;
  245. }
  246.  
  247. } // End of Node class
  248.  
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement