Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import data_structures.*;
- public class P2Driver {
- private Queue<Integer> testQueue;
- private Stack<Integer> testStack;
- private Queue<String> stringQueue;
- private Stack<String> stringStack;
- private int intCount = 100;
- private void runTests() {
- testQueue = new Queue<Integer>();
- testStack = new Stack<Integer>();
- testStack.makeEmpty();
- testQueue.makeEmpty();
- testStack.push(new Integer(1));
- testStack.push(new Integer(3));
- testStack.push(new Integer(5));
- testStack.push(new Integer(7));
- for(Integer i : testStack)
- System.out.println(i);
- System.out.println("\nQuick Queue test (Prints 0):");
- try {
- testQueue.enqueue(new Integer(0));
- System.out.println(testQueue.dequeue());
- testQueue.makeEmpty();
- } catch (Exception e) {
- System.out.println("Error adding to Queue");
- }
- System.out.println("\nQuick Stack test (Prints 0): ");
- try{
- testStack.push(new Integer(0));
- System.out.println(testStack.pop());
- testStack.makeEmpty();
- } catch (Exception e) {
- System.out.println("Error adding to stack");
- }
- try {
- System.out.println("\nTesting Queue: Adding Integers");
- System.out.println("\nLinked List empty (should be true): " + testQueue.isEmpty());
- for(int i = 0 ; i < intCount ; i++ ) {
- testQueue.enqueue(new Integer(i));
- }
- System.out.println("Linked List empty (should be false): " + testQueue.isEmpty());
- System.out.println("Linked List size: " + testQueue.size());
- System.out.println("Peek first: " + testQueue.peek());
- System.out.println("\nTesting Queue: Removing Integers");
- for(int i = 0 ; i < intCount ; i++ ) {
- System.out.println(testQueue.dequeue());
- }
- } catch (Exception e) {
- System.out.println("Error with massive queue");
- }
- try {
- System.out.println("\nTesting Stack: Adding Integers");
- System.out.println("\nLinked List empty (should be true): " + testStack.isEmpty());
- for( int i = 0 ; i < intCount ; i++ )
- testStack.push(new Integer(i));
- System.out.println("Linked List empty (should be false): " + testStack.isEmpty());
- System.out.println("Linked List size: " + testStack.size());
- System.out.println("Peek first: " + testStack.peek());
- System.out.println("\nTesting Queue: Removing Integers");
- while(!testStack.isEmpty())
- System.out.println(testStack.pop());
- } catch (Exception e) {
- System.out.println("Error with massive Stack");
- }
- testQueue.makeEmpty();
- testStack.makeEmpty();
- stringQueue = new Queue<String>();
- stringStack = new Stack<String>();
- String[] arrString = {
- new String("test1"),
- new String("test2"),
- new String("test3"),
- new String("test4"),
- new String("test5")
- };
- System.out.println("\n\nTest Queue with strings:");
- try {
- for(String i : arrString)
- stringQueue.enqueue(i);
- System.out.println("\nStrings in Queue before remove:");
- for(String i : stringQueue)
- System.out.println(i);
- try {
- System.out.println("\nString 'test1' found: " + stringQueue.contains(new String("test1")));
- } catch (Exception e) {
- System.out.println("Error locating string in Stack");
- }
- try {
- System.out.println("String 'test1' Removed: " + stringQueue.remove(new String("test1")));
- } catch (Exception e) {
- System.out.println("Error Removing string in Stack");
- System.out.println(e);
- }
- for(int i = 0 ; i < 4 ; i++)
- System.out.println(stringQueue.dequeue());
- } catch (Exception e) {
- System.out.println("Error with manipulating strings ");
- }
- stringStack.makeEmpty();
- System.out.println("\n\nTest Stack with strings:");
- for(String i : arrString)
- stringStack.push(i);
- System.out.println("\nStrings in Stack before remove:");
- for(String i : stringStack)
- System.out.println(i);
- try {
- System.out.println("\nString 'test1' found: " + stringStack.contains(new String("test1")));
- } catch (Exception e) {
- System.out.println("Error locating string in Stack");
- }
- try {
- System.out.println("String 'test1' Removed: " + stringStack.remove(new String("test1")));
- } catch (Exception e) {
- System.out.println("Error Removing string in Stack");
- }
- for(int i = 0 ; i < 4 ; i++)
- System.out.println(stringStack.pop());
- }
- public P2Driver() {
- runTests();
- }
- public static void main(String [] args) {
- new P2Driver();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement