示例#1
0
  public static void main(String[] args) {
    MyQueue<Integer> my_queue = new MyQueue<Integer>();

    // Let's test our code against a "real" queue
    Queue<Integer> test_queue = new LinkedList<Integer>();

    for (int i = 0; i < 100; i++) {
      int choice = AssortedMethods.randomIntInRange(0, 10);
      if (choice <= 5) { // enqueue
        int element = AssortedMethods.randomIntInRange(1, 10);
        test_queue.add(element);
        my_queue.add(element);
        System.out.println("Enqueued " + element);
      } else if (test_queue.size() > 0) {
        int top1 = test_queue.remove();
        int top2 = my_queue.remove();
        if (top1 != top2) { // Check for error
          System.out.println("******* FAILURE - DIFFERENT TOPS: " + top1 + ", " + top2);
        }
        System.out.println("Dequeued " + top1);
      }

      if (test_queue.size() == my_queue.size()) {
        if (test_queue.size() > 0 && test_queue.peek() != my_queue.peek()) {
          System.out.println(
              "******* FAILURE - DIFFERENT TOPS: "
                  + test_queue.peek()
                  + ", "
                  + my_queue.peek()
                  + " ******");
        }
      } else {
        System.out.println("******* FAILURE - DIFFERENT SIZES ******");
      }
    }
  }
示例#2
0
 public static void main(String[] args) {
   Solution34 sol34 = new Solution34();
   int[] values = new int[] {11, 34, 2, 6, 23, 5, 10};
   System.out.print("Create the queue: ");
   MyQueue queue = sol34.new MyQueue(values);
   sol34.printOutQueue(values);
   System.out.print("Remove three elements: ");
   for (int i = 0; i < 3; i++) {
     System.out.print(queue.remove() + "  ");
   }
   System.out.println();
   System.out.print("Peek the next element: ");
   System.out.print(queue.peek());
   System.out.println();
   System.out.print("Add one element 0 and print out all current queue: ");
   queue.add(0);
   while (!queue.isEmpty()) {
     System.out.print(queue.remove() + "  ");
   }
 }
示例#3
0
 public boolean hasNext() {
   return frontier.peek() != null;
 }