-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAStack.java
More file actions
78 lines (68 loc) · 2.5 KB
/
Copy pathAStack.java
File metadata and controls
78 lines (68 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/** Array-based stack implementation */
class AStack<T> implements Stack<T> {
private static final int defaultSize = 10;
protected int maxSize; //基于数组实现的栈能够容纳的最大栈元素个数。
protected int top; // 指向栈顶的下一个位置。
protected T[] listArray; // 定义泛型类型的数组引用。
AStack() { this(defaultSize); }
@SuppressWarnings("unchecked")
AStack(int size) {
maxSize = size;
top = 0;
listArray = (T[])new Object[size];// 泛型数组的分配方式,这种方式会编译警告,同学们忽略即可。这是Java泛型历史造成的。
}
public void clear() { top = 0; }
public void push(T it) {
if (isFull()){
System.out.println("The stack is full.");
return;
}
listArray[top++] = it; //top指向栈顶的下一个位置
}
public T pop() {
if (isEmpty()){
System.out.println("The stack is empty.");
return null;
}
return listArray[--top];
}
public T topValue() {
if (isEmpty()){
System.out.println("The stack is empty.");
return null;
}
return listArray[top-1];
}
public int length() { return top; }
public boolean isEmpty(){ return top == 0;}
public boolean isFull(){ return top == maxSize;}
public String toString()
{
StringBuilder out = new StringBuilder((length() + 1) * 4);
out.append("< ");
for (int i = top-1; i >= 0; i--) {
out.append(listArray[i]);
out.append(" ");
}
out.append(">");
return out.toString();
}
public static void main(String[] args){
//测试基于数组实现的泛型Stack
//其中一个Stack里面存储的元素类型是Integer,一个Stack里面存储的数据类型是String。
AStack<Integer> intStack = new AStack<>(20);
AStack<String> stringStack = new AStack<>(20);
intStack.push(20);
intStack.push(30);
intStack.push(40);
System.out.println(intStack);
intStack.pop();
System.out.println(intStack);
stringStack.push("hello");
stringStack.push("world");
stringStack.push("everyone!");
System.out.println(stringStack);
stringStack.pop();
System.out.println(stringStack);
}
}