IN JAVA: Stacks and Queues: Railroad cars are to -- enter from the right and are
ID: 3804090 • Letter: I
Question
IN JAVA: Stacks and Queues: Railroad cars are to -- enter from the right and are sent to the output on the left by routing -- through the stack. Each car can be brought into the stack and removed at -- any time. This program aids the switch yard manager in determining the -- ordering of the outgoing cars. The program expects two sequences of cars. -- The first is the incomming sequence, and the second is the desired out-going -- sequence. The program tells the manager if the mapping can be performed -- and the ordering of the pushes and pops. -- For example: -- an input of: 1 2 3 4 can be mapped to an output of 4 3 2 1 using the sequence -- -- push push push push pop pop pop pop -- -- but -- -- an input of: 1 2 3 4 cannot be mapped to an output of 4 2 3 1 -- -- Specifications: -- -- 1. This program will read pairs of input sequences consisting of up to -- 10 strings -- -- 2. The program prints the sequence of push and pop operations that are -- needed to be performed. -- -- 3. If the mapping can be accomplished successfully, outputs: -- -- The input can be mapped to the output -- -- 4. If the mapping cannot be accomplished successfully, outputs: -- -- The input cannot be mapped to the output -- -- The preceeding examples would produce the output: -- -- Attempting to map the input 1 2 3 4 to the output 4 3 2 1 -- The sequence of moves are: push push push push pop pop pop pop -- The input can be mapped to the output -- -- Attempting to map the input 1 2 3 4 to the output 4 2 3 1 -- The sequence of moves are: push push push push pop -- The input cannot be mapped to the output -- -- 5. The program processes all input to the end of the input. -- -- -- Error Handling: -- -- 1. If the user attempts to enter more than the maximum allowable number of -- elements the program outputs: -- -- A maximum of only 10 items are allowed to be input for each sequence - Please re-enter -- -- The input lines are cleared for the next input pair.
Explanation / Answer
package parking;
import java.util.ArrayList;
import java.util.Scanner;
public class SequenceValidater {
static StackOfInts stack=new StackOfInts();
static Scanner read=new Scanner(System.in);
static ArrayList<Integer> inputs=new ArrayList<Integer>();
static ArrayList<Integer> outputs=new ArrayList<Integer>();
static String[] in;
static String[] out;
public static void main(String args[])
{
getinput();
displaymsg();
displayResult();
}
public static void getinput()
{
System.out.println("enter input sequnce with in between");
in=read.nextLine().split(" ");
for(int i=0;i<in.length;i++)
{
inputs.add(Integer.parseInt(in[i])) ;
}
System.out.println("enter output sequnce with space in between");
out=read.nextLine().split(" ");
for(int i=0;i<out.length;i++)
{
outputs.add(Integer.parseInt(out[i])) ;
}
}
public static void displaymsg()
{
System.out.print("attempting to map input sequence ");
for(int i=0;i<inputs.size();i++)
{
System.out.print(inputs.get(i)+" ");
}
System.out.print(" to output sequnce ");
for(int i=0;i<outputs.size();i++)
{
System.out.print(outputs.get(i)+" ");
}
System.out.println();
}
public static void displayResult()
{
for(int i=0;i<inputs.size();i++)
{
putOnStack(inputs.get(i));
}
for(int i=0;i<outputs.size();i++)
{
if(get(outputs.get(i)))
{
break;
}
}
}
public static void putOnStack(int i)
{
stack.push(i);
System.out.print(" push ");
}
public static boolean get(int i)
{
if(stack.pop()==i)
{
System.out.print(" pop ");
return false;
}
else
{
System.out.println(" input cannot be mapped to output");
return true;
}
}
}