Write a java program where you declare and use a Queue and a Stack. Hint: (impor
ID: 3759690 • Letter: W
Question
Write a java program where you declare and use a Queue and a Stack.
Hint: (import the stack and queue libraries) use the library methods: for a stack use push and pop, for a
queue add
a) Declare a queue called “q” and load it with the integers: 10, 20, 30
b) Display the queue by printing it.
c) Remove the first node of the queue, display the queue size, display the queue head after the
remove.
d) Declare a stack called “s” and load it with the integers: 100, 200, 300
e) Display the stack by printing it.
f) Remove a node from the stack, display the stack size, display the stack top after the remove.
Explanation / Answer
Stack<String> stack = new Stack<String>();
stack.push("Test");
...
String next = stack.pop();
Iterator<String> i = queue.iterator();
while (i.hasNext()) {
String s = i.next();
StdOut.println(s);
}
for (String s : queue)
StdOut.println(s);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.