1. Using your preferred IDE or this online IDE, create a Java stack consisting of four (4) book titles entered by the user. Pop the stack's elements one by one; each popped element will be added to a queue. Then, print the content of the queue.
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Stack<String> stack = new Stack<>();
Queue<String> queue = new LinkedList<>();
for (int i = 0; i < 4; i++) {
System.out.println((i + 1) + " Title: ");
stack.push(in.nextLine());
}
for (int i = 0; i < 4; i++) {
queue.add(stack.pop());
}
for (String title : queue) {
System.out.println(title);
}
}
}
Comments
Leave a comment