package l15; /** * @author Lech Szymanski * * Test of the queue implementations */ public class Test{ public static void main(String[] args) { // First test the linked list implementation System.out.println("Testing the SLLQ..."); // Create a new queue of Strings Queue241 q1 = new SLLQ<>(); // Add two items q1.add("apple"); q1.add("pear"); // Show the contents of the queue System.out.println(q1); // Remove the first item System.out.println(q1.remove()); // SHow the contenst of the queue System.out.println(q1); // Now test the circular array implementation System.out.println("\nTesting the CircularQ..."); // Create a new queue of Strings with initial capacity // for 5 items Queue241 q2 = new CircularQ(5); // Add 20 items to the queue, randomly deciding from time to time // to remove an item if the queue is not empty int i = 0; while (i < 20) { // The item is just a string containing the number i String item = " " + i; if (q2.isEmpty()) { // If the queue is empty, add the new item q2.add(item); System.out.println("Added " + i + " " + q2); i++; } else { // If the queue is not empty, generate a random number // between 0 and 1...if the number is less than 0.65 (so 65% of the // time) add the item to the queue, otherwise (35% of the time) remove // an item from the queue if (Math.random() < 0.65) { q2.add(" " + i); System.out.println("Added " + i + " " + q2); i++; } else { System.out.println("Removed " + q2.remove() + " remaining " + q2); } } } // Remove all the items for the queue while (!q2.isEmpty()) { System.out.println("Removed " + q2.remove() + " remaining " + q2); } } }