以前定义的IntegerStack接口,只能用于存放Integer类型的数据。然而对于栈来说,不管内部存放的是什么类型的数据,基本操作与元素的具体类型无关。
1. 编写一个通用的GeneralStack接口,接口中的操作对任何引用类型的数据都适用。
一旦定义完毕,只能存放一种类型的数据,比如只能存放String或只能存放Integer。GeneralStack接口方法如下:
push(item
);
pop();
peek();
public boolean empty();
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, Car4个选项。如果输入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
.LinkedList
;
import java
.util
.Scanner
;
interface GeneralStack<T> {
T
push(T item
);
T
pop();
T
peek();
boolean empty();
int size();
}
class ArrayListGeneralStack implements GeneralStack {
private LinkedList
<Object> list
;
public ArrayListGeneralStack() {
list
= new LinkedList<>();
}
@Override
public String
toString() {
return list
.toString();
}
@Override
public Object
push(Object item
) {
if (item
== null
) {
return null
;
}
list
.add(item
);
return item
;
}
@Override
public Object
pop() {
if (list
.size() == 0) {
return null
;
}
Object topNum
= list
.getLast();
list
.removeLast();
return topNum
;
}
@Override
public Object
peek() {
if (list
.size() == 0) {
return null
;
}
return list
.getLast();
}
@Override
public boolean empty() {
return list
.size() == 0;
}
@Override
public int size() {
return list
.size();
}
}
class Car {
private int id
;
private String name
;
public int getId() {
return id
;
}
public void setId(int id
) {
this.id
= id
;
}
public String
getName() {
return name
;
}
public void setName(String name
) {
this.name
= name
;
}
@Override
public String
toString() {
return "Car [" +
"id=" + id
+
", name=" + name
+
']';
}
}
public class Main {
public static void main(String
[] args
) {
Scanner scanner
= new Scanner(System
.in
);
while (true) {
String choice
= scanner
.next();
if (choice
.equals("quit")) {
break;
}
switch (choice
) {
case "Integer":
System
.out
.println("Integer Test");
ArrayListGeneralStack algsInt
= new ArrayListGeneralStack();
int mInt
= scanner
.nextInt();
int nInt
= scanner
.nextInt();
while (mInt
-- > 0) {
int pushNum
= scanner
.nextInt();
System
.out
.println("push:" + pushNum
);
algsInt
.push(pushNum
);
}
while (nInt
-- > 0) {
System
.out
.println("pop:" + algsInt
.pop());
}
System
.out
.println(algsInt
);
int sumInt
= 0;
int sizeInt
= algsInt
.size();
for (int i
= 0; i
< sizeInt
; i
++) {
sumInt
+= Integer
.parseInt(algsInt
.peek().toString());
algsInt
.pop();
}
System
.out
.println("sum=" + sumInt
);
System
.out
.println(algsInt
.getClass().getInterfaces()[0]);
break;
case "Double":
System
.out
.println("Double Test");
ArrayListGeneralStack algsDouble
= new ArrayListGeneralStack();
int mDouble
= scanner
.nextInt();
int nDouble
= scanner
.nextInt();
while (mDouble
-- > 0) {
double pushNum
= scanner
.nextDouble();
System
.out
.println("push:" + pushNum
);
algsDouble
.push(pushNum
);
}
while (nDouble
-- > 0) {
System
.out
.println("pop:" + algsDouble
.pop());
}
System
.out
.println(algsDouble
);
double sumDouble
= 0;
int sizeDouble
= algsDouble
.size();
for (int i
= 0; i
< sizeDouble
; i
++) {
sumDouble
+= Double
.parseDouble(algsDouble
.peek().toString());
algsDouble
.pop();
}
System
.out
.println("sum=" + sumDouble
);
System
.out
.println(algsDouble
.getClass().getInterfaces()[0]);
break;
case "Car":
System
.out
.println("Car Test");
ArrayListGeneralStack algsCar
= new ArrayListGeneralStack();
int mCar
= scanner
.nextInt();
int nCar
= scanner
.nextInt();
while (mCar
-- > 0) {
int id
= scanner
.nextInt();
String name
= scanner
.next();
Car car
= new Car();
car
.setId(id
);
car
.setName(name
);
System
.out
.println("push:" + car
);
algsCar
.push(car
);
}
while (nCar
-- > 0) {
System
.out
.println("pop:" + algsCar
.pop());
}
System
.out
.println(algsCar
);
int sizeCar
= algsCar
.size();
for (int i
= 0; i
< sizeCar
; i
++) {
Car car
=(Car
) algsCar
.pop();
System
.out
.println(car
.getName());
}
System
.out
.println(algsCar
.getClass().getInterfaces()[0]);
break;
}
}
}
}