package queues_and_stacks; import java.util.*; public class Queues_and_Stacks { public static void main(String[] args) { // Create a stack of strings and add some names Stack stack = new Stack(); String[] names = {"Al", "Bob", "Carol"}; System.out.println("Pushing onto the stack the names:"); System.out.println("Al Bob Carol"); for (String s : names) { stack.push(s); } // Now pop and print everything on the stack String message = "Popping and printing all stack values:"; System.out.println(message); while (!stack.empty()) { System.out.print(stack.pop() + " "); } // ArrayStack Demo String str; // Use for output ArrayStack st = new ArrayStack(5); str = "\n\nPushing 10 20 onto the stack."; System.out.println(str); st.push(10); st.push(20); str = "Value at top of the stack is "; System.out.println(str + st.peek()); str = "Popping and printing all values:"; System.out.println(str); while (!st.empty()) { System.out.print(st.pop() + " "); } // ArrayQueue Demo String str2; // Holds various string values ArrayQueue queue = new ArrayQueue(4); str2 = "\n\nQueue has capacity "; System.out.println(str2 + queue.capacity()); // Add 4 names String[] names2 = {"Alfonso", "Bob", "Carol", "Deborah"}; System.out.println("Adding names: "); for (String s : names) { System.out.print(s + " "); queue.enqueue(s); } System.out.println("\nState of queue is: "); System.out.println(queue); // Remove 2 names System.out.println("Removing 2 names."); queue.dequeue(); queue.dequeue(); System.out.println("State of queue is: "); System.out.println(queue); // Add a name System.out.println("Adding the name Elaine:"); queue.enqueue("Elaine"); System.out.println("State of queue is: "); System.out.println(queue); } }