以前定义的IntegerStack接口,只能用于存放Integer类型的数据。然而对于栈来说,不管内部存放的是什么类型的数据,基本操作与元素的具体类型无关。
1. 编写一个通用的GeneralStack接口,接口中的操作对任何引用类型的数据都适用。
一旦定义完毕,只能存放一种类型的数据,比如只能存放String或只能存放Integer。
GeneralStack
接口方法如下:push(item); //如item为null,则不入栈直接返回null。 pop(); //出栈,如为栈为空,则返回null。 peek(); //获得栈顶元素,如为空,则返回null. public boolean empty();//如为空返回true public int size(); //返回栈中元素数量
2.定义GeneralStack的实现类ArrayListGeneralStack
内部使用ArrayList对象存储,属性名为
list
。方法:
public String toString()
//该方法的代码为return list.toString();
提示:
- 不用使用top指针。
- 直接复用ArrayList中已有的方法。
- pop时应将相应的元素从ArrayList中移除。
- 代码中不要出现类型不安全的强制转换。
3.定义Car对象
属性:
private int id; private String name;
方法:Eclipse自动生成setter/getter,toString方法。
4.main方法说明
- 输入选项,有
quit, Integer, Double, Car
4个选项。如果输入quit,则直接退出。否则,输入整数m与n。m代表入栈个数,n代表出栈个数。然后声明栈变量stack
。- 输入
Integer
,打印Integer Test
。建立可以存放Integer类型的ArrayListGeneralStack
。入栈m次,出栈n次。打印栈的toString方法。最后将栈中剩余元素出栈并累加输出。- 输入
Double
,打印Double Test
。剩下的与输入Integer一样。- 输入
Car
,打印Car Test
。其他操作与Integer、Double基本一样。只不过最后将栈中元素出栈,并将其name依次输出。2、3、4步骤做完都要使用代码
System.out.println(stack.getClass().getInterfaces()[0]);
打印标识信息特别注意:如果栈为空的时候继续出栈,则返回null
输入样例
Integer 5 2 1 2 3 4 5 Double 5 3 1.1 2.0 4.9 5.7 7.2 Car 3 2 1 Ford 2 Cherry 3 BYD quit
输出样例
Integer Test push:1 push:2 push:3 push:4 push:5 pop:5 pop:4 [1, 2, 3] sum=6 interface GeneralStack Double Test push:1.1 push:2.0 push:4.9 push:5.7 push:7.2 pop:7.2 pop:5.7 pop:4.9 [1.1, 2.0] sum=3.1 interface GeneralStack Car Test push:Car [id=1, name=Ford] push:Car [id=2, name=Cherry] push:Car [id=3, name=BYD] pop:Car [id=3, name=BYD] pop:Car [id=2, name=Cherry] [Car [id=1, name=Ford]] Ford interface GeneralStack
import java.util.*;
class Car{
private int id;
private String name;
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String toString(){
return "Car [id=" + getId() + ", name=" + getName() + "]";
}
}
interface GeneralStack{
public Object push(Object item); //如item为null,则不入栈直接返回null。否则直接入栈,然后返回item。
public Object pop(); //出栈,如栈为空,则返回null。
public Object peek(); //获得栈顶元素,如栈顶为空,则返回null。注意:不要出栈
public boolean empty(); //如过栈为空返回true
public int size(); //返回栈中元素数量
}
class ArrayListGeneralStack implements GeneralStack{
ArrayList<Object> list = new ArrayList<Object>();
public ArrayListGeneralStack(){
ArrayList<Object> stack = new ArrayList<Object>();
list = stack;
}
public Object push(Object item){
if(item == null){
return null;
}else{
list.add(list.size(),item);
return item;
}
}
public Object pop(){
if(list.size() == 0){
return null;
}
Object a = list.get(list.size() - 1);
list.remove(list.size() - 1);
return a;
}
public Object peek(){
if(list.size() == 0){
return null;
}else{
return list.get(list.size() - 1);
}
}
public boolean empty(){
if(list.size() == 0){
return true;
}else{
return false;
}
}
public int size(){
return list.size();
}
public String toString(){
return list.toString();
}
}
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i;
while(sc.hasNextLine()){
ArrayListGeneralStack stack = new ArrayListGeneralStack();
String str = sc.nextLine();
if(str.equals("Integer")){
System.out.println("Integer Test");
int m = sc.nextInt();
int n = sc.nextInt();
for(i = 0;i < m;i ++){
System.out.println("push:" + stack.push(sc.nextInt()));
}
int sum = 0;
for(i = 0;i < n;i ++){
System.out.println("pop:" + stack.pop());
}
System.out.println(stack.toString());
while(!stack.empty()){
sum += (int)stack.pop();
}
System.out.println("sum=" + sum);
System.out.println(stack.getClass().getInterfaces()[0]);
sc.nextLine();
}else if(str.equals("Double")){
System.out.println("Double Test");
int m = sc.nextInt();
int n = sc.nextInt();
for(i = 0;i < m;i ++){
System.out.println("push:" + stack.push(sc.nextDouble()));
}
double sum = 0;
for(i = 0;i < n;i ++){
System.out.println("pop:" + stack.pop());
}
System.out.println(stack.toString());
while(!stack.empty()){
sum += (double)stack.pop();
}
System.out.println("sum=" + sum);
System.out.println(stack.getClass().getInterfaces()[0]);
sc.nextLine();
}else if(str.equals("Car")){
System.out.println("Car Test");
int m = sc.nextInt();
Car car[] = new Car[m];
int n = sc.nextInt();
sc.nextLine();
for(i = 0;i < m;i ++){
String arr[] = sc.nextLine().split("\\s+");
car[i] = new Car();
car[i].setId(Integer.parseInt(arr[0]));
car[i].setName(arr[1]);
System.out.println("push:" + stack.push(car[i]));
}
for(i = 0;i < n;i ++){
System.out.println("pop:" + stack.pop());
}
System.out.println(stack.toString());
for(i = m - n - 1;i >= 0;i --){
System.out.println(car[i].getName());
}
while(!stack.empty()){
stack.pop();
}
System.out.println(stack.getClass().getInterfaces()[0]);
}else if(str.equals("quit")){
return;
}
}
}
}