Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Amanda Alvarez
- *
- * Linked List class that implements DataCollection. It also contains a node class
- * within this class since it will be used to keep track of items in the list.
- */
- public class LinkedListDataCollection implements DataCollection {
- /**
- * Creates the selected node and sets it to null.
- */
- private Node selected = null;
- /**
- * Creates a variable to be the head of the node in the linked list.
- */
- private Node head;
- /**
- * Creates the x and y locations for the item.
- */
- private int x, y;
- /**
- * Creates the size of the linked list.
- */
- private int size;
- /**
- * Default constructor
- */
- public LinkedListDataCollection() {
- }
- /**
- * Constructor that takes in an x and y location for an Item.
- */
- public LinkedListDataCollection(int x, int y) {
- this.x = x;
- this.y = y;
- }
- /**
- * Takes in an item and adds it to the list.
- */
- public void add(Item someItem) {
- // Creates a temporary node.
- Node tempNode = new Node(someItem);
- // Sets the head node to the temporary node if it is not null.
- if (head == null)
- head = tempNode;
- // Adds an item to the list.
- else {
- tempNode.setItem(someItem);
- tempNode.setNext(head);
- head = tempNode;
- }
- // Sets the location of item
- someItem.setLocation(x + size * Item.OVERALL_WIDTH, y);
- }
- /**
- * Removes item from the list.
- */
- public void remove(){
- // If the selected node is not null
- if(selected!=null) {
- // Create 2 temporary nodes to keep track of where the nodes are.
- Node previous = null;
- Node current = head;
- // While the current node is not selected
- while (current != selected) {
- previous = current; // Sets the previous node to the current node.
- current = current.getNext(); // Gets the next node to get the new current node.
- }
- // If the previous temporary node is selected
- if (previous != null)
- previous.setNext(current.getNext());
- else
- head = head.getNext();
- }
- // Makes the selected node null.
- selected = null;
- }
- /**
- * This is what changes the node to be highlighted if it equals the item.
- */
- private void changeSelected(Item someItem) {
- // Goes through the list of nodes
- for (Node node = head; node.getNext() != null; node = node.getNext()) {
- // Highlights the node if it is equal to the item.
- if (node.getItem() == someItem) {
- (node.getItem()).highlight(true);
- }
- // Does not highlight the node.
- else
- (node.getItem()).highlight(false);
- }
- }
- /**
- * The selected is reset to the head of the linked list.
- */
- public void reset() {
- selected = head;
- }
- /**
- * Takes in an item and resets the selected to be that item.
- */
- public void reset(Item someItem) {
- // Creates a temporary node that will take the place of the head node in the linked list.
- Node tempNode = head;
- // Checks to see if the passed in item is not null.
- if (someItem != null) {
- // Loops through the list while the temporary node is not null
- // and the item in the current node is not the item being passed.
- while (tempNode.getItem() != someItem && tempNode != null ) {
- // Sets the temporary node to the next node in the list
- tempNode = tempNode.getNext();
- }
- // Sets the selected node to temporary node.
- selected = tempNode;
- changeSelected(selected.getItem());
- }
- // Sets selected to null
- else
- selected = null;
- }
- /**
- * Checks to see if there is another item in the list and returns it.
- */
- public Item next() {
- Item item;
- if (head != null) {
- item = selected.getItem();
- selected = selected.getNext();
- }
- else
- item = null;
- return item;
- }
- /**
- * Checks to see if the list has a next item. If
- * the head is not null and the selected is not null,
- * it returns true.
- */
- public boolean hasNext() {
- return (selected != null) && (head != null) ;
- }
- /**
- * Paints each individual item in the list.
- */
- public void paint(Graphics pane) {
- int items = 0; // Used to move the items over.
- Node temp = head; // Temporary node.
- // Checks to see if the head of the list is not not.
- if (head != null)
- {
- // While the temporary node is not null.
- while(temp != null)
- {
- // Moves items over continuously from the list.
- int move = x + items * Item.OVERALL_WIDTH;
- temp.getItem().setX(move);
- temp.getItem().paint(pane);
- temp = temp.getNext();
- items++;
- }
- }
- }
- /**
- * Inner class that creates the nodes.
- */
- private class Node {
- private Item item; // Data that is carried by this node.
- private Node next; // Reference to the next node in the list.
- /**
- * Constructor that sets both the item and next Objects as null and takes in some
- * item and sets it to the item that is being used in this class.
- */
- public Node(Item someItem){
- item = null;
- next = null;
- item = someItem;
- }
- /**
- * Gets the next node in the list.
- */
- public Node getNext(){
- return next;
- }
- /**
- * Sets the next node to some node passed to it.
- */
- public void setNext(Node someNode){
- next = someNode;
- }
- /**
- * Gets the next item in the list.
- */
- public Item getItem()
- {
- return item;
- }
- /**
- * Sets the next item to the item passed to it.
- */
- public void setItem(Item someItem)
- {
- item = someItem;
- }
- } // End of Node class
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement