Advertisement
Guest User

Linked List: occurrences of number

a guest
Sep 24th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.51 KB | None | 0 0
  1. public class SortedBag<T> implements BagInterface<T>{
  2.  
  3.     private Node firstNode;
  4.     private int numberOfNodes = 0;
  5.  
  6.     public SortedBag() {
  7.         firstNode = null;
  8.         numberOfNodes = 0;
  9.     }
  10.  
  11.     @Override
  12.     public int getCurrentSize() {
  13.         return numberOfNodes;
  14.     }
  15.  
  16.     @Override
  17.     public boolean isEmpty() {
  18.         return numberOfNodes == 0;
  19.     }
  20.  
  21.     @Override
  22.     public boolean add(T newEntry) {
  23.         Node newNode = new Node(newEntry);
  24.         Node currentNode = firstNode;
  25.         Node prevNode = firstNode;
  26.  
  27.         // We are using a coinValue of 0 to tell if it is invalid
  28.         if (newNode.getValue((Coins) newNode.getCoin()) == 0) {
  29.             return false;
  30.         }
  31.  
  32.         // firstNode is null, so we don't need to specify newNode.next = firstNode
  33.         if (isEmpty()) {
  34.             firstNode = newNode;
  35.             numberOfNodes++;
  36.             return true;
  37.         }
  38.  
  39.         // if the first node is bigger, we can't use prevNode
  40.         if (newNode.getValue((Coins) newNode.getCoin()).compareTo(
  41.                 firstNode.getValue((Coins) firstNode.getCoin())) < 0) {
  42.             newNode.next = firstNode;
  43.             firstNode = newNode;
  44.             numberOfNodes++;
  45.             return true;
  46.         }
  47.  
  48.         for (int i = 0; i < numberOfNodes; i++) {
  49.             // newNode is smaller than currentNode, so place it in front of currentNode
  50.             if (newNode.getValue((Coins) newNode.getCoin()).compareTo(
  51.                     currentNode.getValue((Coins) currentNode.getCoin())) < 0) {
  52.                 prevNode.next = newNode;
  53.                 newNode.next = currentNode;
  54.                 currentNode = newNode;
  55.                 numberOfNodes++;
  56.                 return true;
  57.             } else {
  58.                 // We've gone through the whole list, so newNode is biggest and should be last
  59.                 if (i == numberOfNodes - 1) {
  60.                     currentNode.next = newNode;
  61.                     numberOfNodes++;
  62.                     return true;
  63.                 }
  64.             }
  65.             // newNode was not smaller than currentNode, so check the next one
  66.             prevNode = currentNode;
  67.             currentNode = currentNode.next;
  68.         }
  69.         return false;
  70.     }
  71.  
  72.     // TODO: supposed to remove last node, not first
  73.     @Override
  74.     public T remove() {
  75.         T result = null;
  76.         if (firstNode != null) {
  77.             result = firstNode.coin;
  78.             firstNode = firstNode.next; // remove first node from chain
  79.             numberOfNodes--;
  80.         } // end if
  81.         return result;
  82.     }
  83.  
  84.     private Node getReferenceTo(T anEntry) {
  85.         boolean found = false;
  86.         Node currentNode = firstNode;
  87.         while (!found && (currentNode != null)) {
  88.             if (anEntry.equals(currentNode.coin)) {
  89.                 found = true;
  90.             } else {
  91.                 currentNode = currentNode.next;
  92.             }
  93.         } // end while
  94.         return currentNode;
  95.     } // end getReferenceTo
  96.  
  97.     // TODO: Needs adjusting to fit our model
  98.     @Override
  99.     public boolean remove(T anEntry) {
  100.         boolean result = false;
  101.         Node nodeN = getReferenceTo(anEntry);
  102.         if (nodeN != null) {
  103.             nodeN.coin = firstNode.coin; // replace located entry with entry
  104.             // in first node
  105.             remove(); // remove first node
  106.             result = true;
  107.         } // end if
  108.         return result;
  109.     }
  110.  
  111.     @Override
  112.     public void clear() {
  113.         while (!isEmpty()) {
  114.             remove();
  115.         }
  116.     }
  117.  
  118.     // TODO: Needs adjusting to fit our model
  119.     @Override
  120.     public int getFrequencyOf(T anEntry) {
  121.         int frequency = 0;
  122.  
  123.         Node currentNode = firstNode;
  124.  
  125.         while (currentNode != null) {
  126.  
  127.             if (anEntry.equals(currentNode.coin))// anEntry.equals(currentNode.coin)
  128.             {
  129.                 frequency++;
  130.             }
  131.             currentNode = currentNode.next;
  132.         } // end while
  133.         return frequency;
  134.     }
  135.  
  136.  
  137.  
  138.     // TODO: Needs adjusting to fit our model
  139.     @Override
  140.     public boolean contains(T anEntry) {
  141.         boolean found = false;
  142.         Node currentNode = firstNode;
  143.  
  144.         while (!found && (currentNode != null)) {
  145.             if (anEntry.equals(currentNode.coin)) {
  146.                 found = true;
  147.             } else {
  148.                 currentNode = currentNode.next;
  149.             }
  150.         } // end while
  151.         return found;
  152.     }
  153.  
  154.     @Override
  155.     public T[] toArray() {
  156.         T[] result = (T[]) new Object[numberOfNodes]; // unchecked cast
  157.         int index = 0;
  158.         Node currentNode = firstNode;
  159.         while ((index < numberOfNodes) && (currentNode != null)) {
  160.             result[index] = currentNode.coin;
  161.             index++;
  162.             currentNode = currentNode.next;
  163.         } // end while
  164.         return result;
  165.     }
  166.  
  167.     @Override
  168.     public boolean isFull() {
  169.         return false;
  170.     }
  171.  
  172.     public class Node {
  173.  
  174.         public T getCoin() {
  175.             return coin;
  176.         }
  177.  
  178.         public void setCoin(T coin) {
  179.             this.coin = coin;
  180.         }
  181.  
  182.         public Node getNext() {
  183.             return next;
  184.         }
  185.  
  186.         public void setNext(Node next) {
  187.             this.next = next;
  188.         }
  189.  
  190.         // Bridge method so we can use the coin value
  191.         public Integer getValue(Coins coin) {
  192.             return coin.getCoinValue();
  193.         }
  194.  
  195.         T coin;
  196.         Node next;
  197.  
  198.         Node(T coin) {
  199.             this(coin, null);
  200.         }
  201.  
  202.         Node(T coin, Node next) {
  203.             this.coin = coin;
  204.             this.next = next;
  205.         }
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement