Beispiel #1
0
 private static void main(String[] args) {
   RandomizedQueue<String> rq = new RandomizedQueue<String>();
   rq.enqueue("hello");
   rq.enqueue(", ");
   rq.enqueue("world");
   rq.enqueue("!");
   System.out.println(rq);
   System.out.printf("size: %d\n", rq.size());
   System.out.printf("sample %s\n", rq.sample());
   System.out.printf("sample %s\n", rq.sample());
   System.out.printf("dequeue %s\n", rq.dequeue());
   System.out.printf("dequeue %s\n", rq.dequeue());
   System.out.println(rq);
   System.out.printf("size: %d\n", rq.size());
 }
  public static void main(String[] args) {
    // unit testing
    RandomizedQueue<String> queue = new RandomizedQueue<String>();
    queue.enqueue("Kabeer");
    queue.enqueue("is");
    queue.enqueue("a");
    queue.enqueue("boy");
    queue.enqueue("dog ");

    String w = queue.dequeue();
    System.out.println("1: " + w);
    System.out.println(queue.size());

    w = queue.dequeue();
    System.out.println("2: " + w);
    System.out.println(queue.size());
    w = queue.dequeue();
    System.out.println("3: " + w);
    System.out.println(queue.size());
    w = queue.dequeue();
    System.out.println("4: " + w);
    System.out.println(queue.size());
    w = queue.dequeue();
    System.out.println("5: " + w);
    System.out.println(queue.size());
    //        for(String x:queue) {
    //            System.out.print(x+" ");
    //        }
    //        System.out.println(" ");
    //
    //
    //        for(String x:queue) {
    //            System.out.print(x+" ");
    //        }
    //
    //        System.out.println(" ");
    //
    //
    //        for(String x:queue) {
    //            System.out.print(x+" ");
    //        }
  }
Beispiel #3
0
  public static void main(String[] args) {
    RandomizedQueue<String> rq = new RandomizedQueue<String>();
    int count = Integer.parseInt(args[0]);

    while (!StdIn.isEmpty()) {
      rq.enqueue(StdIn.readString());
    }
    for (int i = 0; i < count; i++) {
      System.out.println(rq.dequeue());
    }
  }
Beispiel #4
0
 public static void main(String[] args) {
   RandomizedQueue<String> randq = new RandomizedQueue<String>();
   int k = Integer.parseInt(args[0]);
   while (!StdIn.isEmpty()) {
     String item = StdIn.readString();
     randq.enqueue(item);
   }
   for (int i = 0; i < k; i++) {
     StdOut.println(randq.dequeue());
   }
 }
Beispiel #5
0
  public static void main(String[] args) {
    int k = Integer.parseInt(args[0]);
    RandomizedQueue<String> queue = new RandomizedQueue<String>();

    while (!StdIn.isEmpty()) {
      queue.enqueue(StdIn.readString());
    }

    for (int i = 0; i < k; i++) {
      System.out.println(queue.dequeue());
    }
  }
 public static void main(String[] args) {
   // unit testing
   RandomizedQueue<Integer> rq = new RandomizedQueue<Integer>();
   for (int i = 0; i < 10; i++) {
     rq.enqueue(i);
   }
   for (Integer s : rq) StdOut.println(s.intValue());
   StdOut.print("\n");
   for (int i = 0; i < 10; i++) {
     StdOut.println(rq.dequeue().intValue());
   }
 }
Beispiel #7
0
  public static void main(String[] args) {
    int numberOfInput = Integer.parseInt(args[0]);
    RandomizedQueue<String> test = new RandomizedQueue<String>();

    while (!StdIn.isEmpty()) {
      test.enqueue(StdIn.readString());
    }

    for (int i = 0; i < numberOfInput; i++) {
      StdOut.println(test.dequeue());
    }
  }
Beispiel #8
0
 public static void main(String[] args) {
   int k = Integer.parseInt(args[0]);
   String input = StdIn.readLine();
   String[] strings = input.split(" ");
   RandomizedQueue q = new RandomizedQueue();
   for (int i = 0; i < strings.length; i++) {
     q.enqueue(strings[i]);
   }
   for (int i = 0; i < k; i++) {
     System.out.println(q.dequeue());
   }
 }
Beispiel #9
0
  public static void main(String[] args) {
    int k = Integer.parseInt(args[0]);
    RandomizedQueue<String> queue = new RandomizedQueue<>();
    while (!StdIn.isEmpty()) {
      queue.enqueue(StdIn.readString());
    }

    Iterator<String> itr = queue.iterator();
    for (int i = 0; i < k; i++) {
      System.out.println(itr.next());
    }
  }
Beispiel #10
0
  /**
   * Unit testing
   *
   * @param args the program arguments
   */
  public static void main(String[] args) {
    RandomizedQueue<String> queue = new RandomizedQueue<>();

    queue.enqueue("rieck");
    queue.enqueue("andrew");
    queue.enqueue("thomas");

    assert (queue.size() == 3);
    assert (!queue.isEmpty());

    for (String t : queue) {
      StdOut.println(t);
    }

    queue.sample();

    queue.dequeue();
    queue.dequeue();
    queue.dequeue();

    assert (queue.isEmpty());

    queue.enqueue("lily");
    queue.enqueue("caleb");
    queue.enqueue("aaron");

    assert (queue.size() == 3);
    assert (!queue.isEmpty());

    int i = 0;
    for (String t : queue) {
      StdOut.println(t);
      for (String u : queue) {
        StdOut.println("   " + u);
        i++;
      }
    }

    assert (i == 9);
  }
  public static void main(String[] args) {

    RandomizedQueue<String> queue = new RandomizedQueue<String>();
    int k = Integer.valueOf(args[0]);
    while (!StdIn.isEmpty()) {
      String tmp = StdIn.readString();
      queue.enqueue(tmp);
    }

    while (k > 0) {
      StdOut.println(queue.dequeue());
      k--;
    }
  }
Beispiel #12
0
  public static void main(String[] args) {
    String s;

    RandomizedQueue<String> rq = new RandomizedQueue<String>();
    int elements = Integer.parseInt(args[0]);

    while (!StdIn.isEmpty()) {
      rq.enqueue(StdIn.readString());
    }

    for (int i = 0; i < elements; i++) {
      StdOut.println(rq.dequeue());
    }
  }
Beispiel #13
0
 public static void main(String[] args) {
   int k = Integer.parseInt(args[0]);
   RandomizedQueue<String> queue = new RandomizedQueue<>();
   String string;
   try {
     while ((string = StdIn.readString()) != null) {
       queue.enqueue(string);
     }
   } catch (NoSuchElementException e) {
     // ok
   }
   for (int i = 0; i < k; i++) {
     StdOut.println(queue.dequeue());
   }
 }
Beispiel #14
0
  public static void main(String[] args) {
    int numOutputs = Integer.parseInt(args[0]);
    RandomizedQueue<String> rq = new RandomizedQueue<String>();

    String[] tokens = StdIn.readAllStrings();

    int numTokens = tokens.length;
    for (int i = 0; i < numOutputs; i++) {
      int r = StdRandom.uniform(numTokens);

      rq.enqueue(tokens[r]);
      tokens[r] = tokens[numTokens - 1];
      tokens[numTokens - 1] = null;
      numTokens--;
    }

    for (int i = 0; i < numOutputs; i++) {
      System.out.println(rq.dequeue());
    }
  }
Beispiel #15
0
 public static void main(String[] args) {
   RandomizedQueue<String> q = new RandomizedQueue<String>();
   while (!StdIn.isEmpty()) q.enqueue(StdIn.readString());
   for (int i = 0; i < (Integer.parseInt(args[0])); i++) StdOut.println(q.dequeue());
 }
  public static void main(String[] args) {
    // Test case: NullPointer when adding null to RandomizedQueue.
    RandomizedQueue<String> rq = new RandomizedQueue<String>();
    try {
      String str = null;
      rq.enqueue(str);
      StdOut.println("Test should throw java.lang.NullPointerException: " + "FAIL!");
    } catch (java.lang.NullPointerException e) {
      StdOut.println("Test should throw java.lang.NullPointerException: " + "Successfull!");
    }

    // Test case: Throw java.util.NoSuchElementException when randomizedQueue
    //            is empty and someone tries to remove an item.
    rq = new RandomizedQueue<String>();
    try {
      rq.dequeue();
      StdOut.println("Test should throw " + "java.util.NoSuchElementException: FAIL!");
    } catch (java.util.NoSuchElementException e) {
      StdOut.println("Test should throw " + "java.util.NoSuchElementException: Successfull!");
    }

    // Test case: Add some stuff and show samples.
    rq = new RandomizedQueue<String>();
    // Make  0,1,2,3,4,5,6,7
    rq.enqueue("7");
    rq.enqueue("6");
    rq.enqueue("5");
    rq.enqueue("4");
    rq.enqueue("3");
    rq.enqueue("2");
    rq.enqueue("1");
    rq.enqueue("0");

    // Sample should not remove entries.
    for (int i = 0; i < 10; i++) {
      StdOut.println(rq.sample());
    }
    StdOut.println("Next case.");
    // dequeue should remove entries in random order.
    for (int i = 0; i < 8; i++) {
      String item = rq.dequeue();
      StdOut.println(item);
    }

    try {
      rq.dequeue();
      StdOut.println("Test should throw " + "java.util.NoSuchElementException: FAIL!");
    } catch (java.util.NoSuchElementException e) {
      StdOut.println("Test should throw java.util." + "NoSuchElementException: Successfull!");
    }

    for (int i = 0; i < 2; i++) {
      // Make  0,1,2,3,4,5,6,7
      rq.enqueue("7");
      rq.enqueue("6");
      rq.enqueue("5");
      rq.enqueue("4");
      rq.enqueue("3");
      rq.enqueue("2");
      rq.enqueue("1");
      rq.enqueue("0");

      for (String s : rq) StdOut.println(s);
      StdOut.println();
    }
  }