Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SortedBag<T> implements BagInterface<T>{
- private Node firstNode;
- private int numberOfNodes = 0;
- public SortedBag() {
- firstNode = null;
- numberOfNodes = 0;
- }
- @Override
- public int getCurrentSize() {
- return numberOfNodes;
- }
- @Override
- public boolean isEmpty() {
- return numberOfNodes == 0;
- }
- @Override
- public boolean add(T newEntry) {
- Node newNode = new Node(newEntry);
- Node currentNode = firstNode;
- Node prevNode = firstNode;
- // We are using a coinValue of 0 to tell if it is invalid
- if (newNode.getValue((Coins) newNode.getCoin()) == 0) {
- return false;
- }
- // firstNode is null, so we don't need to specify newNode.next = firstNode
- if (isEmpty()) {
- firstNode = newNode;
- numberOfNodes++;
- return true;
- }
- // if the first node is bigger, we can't use prevNode
- if (newNode.getValue((Coins) newNode.getCoin()).compareTo(
- firstNode.getValue((Coins) firstNode.getCoin())) < 0) {
- newNode.next = firstNode;
- firstNode = newNode;
- numberOfNodes++;
- return true;
- }
- for (int i = 0; i < numberOfNodes; i++) {
- // newNode is smaller than currentNode, so place it in front of currentNode
- if (newNode.getValue((Coins) newNode.getCoin()).compareTo(
- currentNode.getValue((Coins) currentNode.getCoin())) < 0) {
- prevNode.next = newNode;
- newNode.next = currentNode;
- currentNode = newNode;
- numberOfNodes++;
- return true;
- } else {
- // We've gone through the whole list, so newNode is biggest and should be last
- if (i == numberOfNodes - 1) {
- currentNode.next = newNode;
- numberOfNodes++;
- return true;
- }
- }
- // newNode was not smaller than currentNode, so check the next one
- prevNode = currentNode;
- currentNode = currentNode.next;
- }
- return false;
- }
- // TODO: supposed to remove last node, not first
- @Override
- public T remove() {
- T result = null;
- if (firstNode != null) {
- result = firstNode.coin;
- firstNode = firstNode.next; // remove first node from chain
- numberOfNodes--;
- } // end if
- return result;
- }
- private Node getReferenceTo(T anEntry) {
- boolean found = false;
- Node currentNode = firstNode;
- while (!found && (currentNode != null)) {
- if (anEntry.equals(currentNode.coin)) {
- found = true;
- } else {
- currentNode = currentNode.next;
- }
- } // end while
- return currentNode;
- } // end getReferenceTo
- // TODO: Needs adjusting to fit our model
- @Override
- public boolean remove(T anEntry) {
- boolean result = false;
- Node nodeN = getReferenceTo(anEntry);
- if (nodeN != null) {
- nodeN.coin = firstNode.coin; // replace located entry with entry
- // in first node
- remove(); // remove first node
- result = true;
- } // end if
- return result;
- }
- @Override
- public void clear() {
- while (!isEmpty()) {
- remove();
- }
- }
- // TODO: Needs adjusting to fit our model
- @Override
- public int getFrequencyOf(T anEntry) {
- int frequency = 0;
- Node currentNode = firstNode;
- while (currentNode != null) {
- if (anEntry.equals(currentNode.coin))// anEntry.equals(currentNode.coin)
- {
- frequency++;
- }
- currentNode = currentNode.next;
- } // end while
- return frequency;
- }
- // TODO: Needs adjusting to fit our model
- @Override
- public boolean contains(T anEntry) {
- boolean found = false;
- Node currentNode = firstNode;
- while (!found && (currentNode != null)) {
- if (anEntry.equals(currentNode.coin)) {
- found = true;
- } else {
- currentNode = currentNode.next;
- }
- } // end while
- return found;
- }
- @Override
- public T[] toArray() {
- T[] result = (T[]) new Object[numberOfNodes]; // unchecked cast
- int index = 0;
- Node currentNode = firstNode;
- while ((index < numberOfNodes) && (currentNode != null)) {
- result[index] = currentNode.coin;
- index++;
- currentNode = currentNode.next;
- } // end while
- return result;
- }
- @Override
- public boolean isFull() {
- return false;
- }
- public class Node {
- public T getCoin() {
- return coin;
- }
- public void setCoin(T coin) {
- this.coin = coin;
- }
- public Node getNext() {
- return next;
- }
- public void setNext(Node next) {
- this.next = next;
- }
- // Bridge method so we can use the coin value
- public Integer getValue(Coins coin) {
- return coin.getCoinValue();
- }
- T coin;
- Node next;
- Node(T coin) {
- this(coin, null);
- }
- Node(T coin, Node next) {
- this.coin = coin;
- this.next = next;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement