-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDynStack.java
More file actions
38 lines (32 loc) · 854 Bytes
/
DynStack.java
File metadata and controls
38 lines (32 loc) · 854 Bytes
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
package chapter9;
public class DynStack implements IntStack {
private int stck[];
private int tos;
DynStack(int size) {
stck = new int[size];
tos = -1;
}
//push an item onto the stack
public void push(int item) {
//if stack is full allocate a larger stack
if (tos == stck.length - 1) {
int temp[] = new int[stck.length * 2];//double size
for (int i = 0; i < stck.length; i++) {
temp[i] = stck[i];
}
stck = temp;
stck[++tos] = item;
} else {
stck[++tos] = item;
}
}
//pop an item from the stack
public int pop() {
if (tos < 0) {
System.out.println("Stack underflow");
return 0;
} else {
return stck[tos--];
}
}
}