⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trainmover.java

📁 stack code impletemtn by java
💻 JAVA
字号:
/**
 * TrainMover.java
 * @version 1.0.0
 * @author Hao Chen
 * 0236407
 */

/**
 * This is a program which simulates this train switching using one or more instances of
 * the MyStack class.Put the inordered trains to a order trains.
 *
 */


public class TrainMover
{
	public static void main(String[] args)//the main method
	{
		int argsL = args.length;//the size of the input array
		MyStack spur = new MyStack();//object stack of MyStack
		MyStack input = new MyStack();
		MyStack output = new MyStack();
		String[] track = new String[2*argsL];//declare the array of strings about the output string

		for ( int i = 0; i < argsL; i++ )//put all inputs into an array
		{
			input.push(args[i]);
		}


		for (int i = 0; i < 2*argsL; i++)//find the outputs
		{

			if (spur.isEmpty())//if spur is empty, then put top of input into spur
			{
				spur.push(input.pop());
				track[i] = "Spur";

			}
			else if(input.isEmpty())//if input is empty, then put top of spur into output
			{
				output.push(spur.pop());
				track[i] = "output";

		    }
			else if (spur.isEmpty()&&input.isEmpty())////if input and spur is empty,then program is over
			    break;

			else
			{
				if(((String)input.top()).compareTo((String)spur.top()) > 0)//if top of input greater than spur
				{

					spur.push(input.pop());
					track[i] = "Spur";
				}else//if top of spur greater than output
				 {
					 int q= input.size();
					 MyStack stor= new MyStack(q);
					 for(int a=0; a<q; a++){//the "no" situation

	                    if(((String)spur.top()).compareTo((String)input.top()) < 0){
                           System.out.println("No.");
						   return;
					    }
					 stor.push(input.pop());
				     }

				     while(!(stor.isEmpty()))//put the values back into input
				     {
						input.push(stor.pop());
				     }

  	                 output.push(spur.pop());
		             track[i] = "output";//output the results

				 }


		     }
	      }

		System.out.println("Yes.");
		for (int i = 0; i < 2*argsL; i++)//print out
		{
			System.out.println(track[i]);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -