SHARE
TWEET

Untitled

a guest Jan 24th, 2020 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Camisha Mortensen
  2. // mortensc
  3. // import java.security.DrbgParameters.NextBytes;
  4. import java.util.Iterator;
  5.  
  6. public class MarbleBag implements Iterable<Marble> {
  7.     private Node first; // first node in list
  8.  
  9.     private class Node {
  10.         Marble marble;
  11.         Node next;
  12.     }
  13.  
  14.     public void add(Marble marble) { // same as push() in Stack
  15.         // Checks if the new marble already exists
  16.         Iterator<Marble> it = this.iterator();
  17.         while (it.hasNext()) {
  18.             if (marble.equals(it.next())) {
  19.                 System.out.println("This marble is already in the bag");
  20.                 return;
  21.             }
  22.         }
  23.  
  24.         // Adds the new marble to the bag
  25.         Node oldfirst = first;
  26.         first = new Node();
  27.         first.marble = marble;
  28.         first.next = oldfirst;
  29.     }
  30.  
  31.     public void del(Marble marble) { // same as push() in Stack
  32.         // Checks if the new marble already exists
  33.         Iterator<Marble> it = this.iterator();
  34.         while (it.hasNext()) {
  35.                 Node currentNode = it.getCurrentNode();
  36.                 Marble currentMarble = it.next();
  37.             if (marble.equals(currentMarble)) {
  38.                 currentNode.next = currentNode.next.next;
  39.             }
  40.         }
  41.  
  42.  
  43.     }
  44.  
  45.     public void inBag() {
  46.         Iterator<Marble> it = this.iterator();
  47.         while (it.hasNext()) {
  48.             Marble currentMarb = it.next();
  49.             System.out.println(currentMarb.getName());
  50.         }
  51.     }
  52.  
  53.     public Iterator<Marble> iterator() {
  54.         return new ListIterator();
  55.     }
  56.  
  57.     public class ListIterator implements Iterator<Marble> {
  58.         public Node current = first;
  59.  
  60.         public boolean hasNext() {
  61.             return current != null;
  62.         }
  63.  
  64.         public void remove() {
  65.             // put stuff here
  66.         }
  67.  
  68.         public Marble next() {
  69.             Marble marble = current.marble;
  70.             current = current.next;
  71.             return marble;
  72.         }
  73.  
  74.     }
  75.  
  76. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top