I need to remove the declaredstack from the code and replace it with the system
ID: 3615002 • Letter: I
Question
I need to remove the declaredstack from the code and replace it with the system stack throughuse of recursion. There should be no stack used directly by thecode, only indirectly through recursion. we do not need to changethe code in any other way-- simply take out the stack and replaceit with recursive calls to the method (making sure that they willstop at some point) that perform the task.
thanks,
/**
* This class takes a string as input and reverses the orderof the
* sentences using stacks and queues
*/
import java.util.Scanner;
import java.util.Stack;
import java.util.Queue;
import java.util.LinkedList;
public class ReverseSentences
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in); //Declare and allocate scanner
Stack<Queue<String>> sq =new Stack(); //Declares and allocates a stack ofqueues
Queue<String> q = newLinkedList(); //Declares and allocates a queueof strings using a linked list
System.out.print("Enter text to bereversed: "); //Prompts user toenter input
Scanner in = newScanner(input.nextLine()); //Reads text from console
while (in.hasNext()) { //loop through all words of text
String nextToken = in.next(); //loads next word intostring variable
q.offer(nextToken); //enqueues word onto queue
if (nextToken.contains(".") ||nextToken.contains("?") || nextToken.contains("!")) { //checks every word for ".", "?", or "!"
sq.push(q); // push queue onto stack
q = new LinkedList(); //allocatesa new queue
}
}
//print thereversed text
System.out.print("Reversedtext: ");
while (!sq.isEmpty()) { //if stack is notempty, pop queue off stack
q = sq.pop();
while (q.peek()!=null) { //while queue q is notempty, dequeue each entry and print
System.out.print(q.poll() + " ");
}
}
}
}
I need to remove the declaredstack from the code and replace it with the system stack throughuse of recursion. There should be no stack used directly by thecode, only indirectly through recursion. we do not need to changethe code in any other way-- simply take out the stack and replaceit with recursive calls to the method (making sure that they willstop at some point) that perform the task.
thanks,
/**
* This class takes a string as input and reverses the orderof the
* sentences using stacks and queues
*/
import java.util.Scanner;
import java.util.Stack;
import java.util.Queue;
import java.util.LinkedList;
public class ReverseSentences
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in); //Declare and allocate scanner
Stack<Queue<String>> sq =new Stack(); //Declares and allocates a stack ofqueues
Queue<String> q = newLinkedList(); //Declares and allocates a queueof strings using a linked list
System.out.print("Enter text to bereversed: "); //Prompts user toenter input
Scanner in = newScanner(input.nextLine()); //Reads text from console
while (in.hasNext()) { //loop through all words of text
String nextToken = in.next(); //loads next word intostring variable
q.offer(nextToken); //enqueues word onto queue
if (nextToken.contains(".") ||nextToken.contains("?") || nextToken.contains("!")) { //checks every word for ".", "?", or "!"
sq.push(q); // push queue onto stack
q = new LinkedList(); //allocatesa new queue
}
}
//print thereversed text
System.out.print("Reversedtext: ");
while (!sq.isEmpty()) { //if stack is notempty, pop queue off stack
q = sq.pop();
while (q.peek()!=null) { //while queue q is notempty, dequeue each entry and print
System.out.print(q.poll() + " ");
}
}
}
}