Write a java program where you declare and use a Queue and a Stack. Hint: (impor
ID: 3759713 • 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<Integer> stack = new Stack<Integer>();
stack.push(17); // auto-boxing (int -> Integer)
int i = stack.pop(); // auto-unboxing (Integer -> int)
Integer a1 = 100;
Integer a2 = 100;
System.out.println(a1 == a2); // true
Integer b1 = new Integer(100);
Integer b2 = new Integer(100);
System.out.println(b1 == b2); // false
Integer c1 = 150;
Integer c2 = 150;
System.out.println(c1 == c2); // false
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.