1.《栈》
利用AarrayList作为数据域实现栈。在主方法main 中输入任意5个串,然后倒序输出。
输入要求
多组输入:
串1 串2 串3 串4 串5
输出要求
串5 串4 串3 串2 串1
输入
无
输出
无
代码
import java.util.ArrayList;
import java.util.Scanner;
public class MyStack {
/***## 09f4c976f4f43e24 ##***/
ArrayList<Object> mylist;
int low=0;
MyStack(){
mylist=new ArrayList<>();
}
private Object pop(){
Object oj=mylist.get(mylist.size()-1);
mylist.remove(mylist.size()-1);
return oj;
}
private void push(Object oj){
mylist.add(oj);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
final int MAX_VALUE = 5;
Scanner input = new Scanner(System.in);
while (input.hasNext()) {
int count = MAX_VALUE;
MyStack stack = new MyStack();
while (count > 0) {
stack.push(input.next());
count--;
}
count = MAX_VALUE;
while (count > 0) {
System.out.print(stack.pop());
if (count != 1)
System.out.print(" ");
count--;
}
System.out.println();
}
}
}