Write a Java implementation for the following problem. You can make use of which
ID: 3807913 • Letter: W
Question
Write a Java implementation for the following problem. You can make use of whichever of the ADTs Stack, Queue, and Deque that you need. Assume that these ADTs are already implemented, compiled and available in files Stack.class, Queue.class, Deque.class.
Input is read from the terminal (System.in). All input Strings are read and stored. The input String PRINT has a special meaning. Whenever a String that matches PRINT is read, the first stored string and the last 5 stored strings are printed. If there are not enough strings, the program should terminate as soon as it runs out of strings.
For example, if the input is:
1 2 3 4 5 6 7 8 PRINT 9 PRINT 10 11 12 13 PRINT
the output would be:
1 8 7 6 5 4 2 9 3
Explanation / Answer
//PrintClass.java Assuming implemented code of Queue pushing all input elements and pop out all after getting PRINT input and displaying first and last five elements public class PrintClass { public static void main(String args[]) { Scanner input = new Scanner(System.in); String n = input.next(); Queue queue = new Queue(); if(!n.equals("PRINT")){ queue.push(n); } else{ queue.display(rear , rear);//to display first element queue.pop(rear); queue.display(top , top-5);//sending parameters to display data from top to top-5 for(int i=1;iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.