-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackClone.java
More file actions
61 lines (50 loc) · 1.1 KB
/
StackClone.java
File metadata and controls
61 lines (50 loc) · 1.1 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
package CC;
public class StackClone<T> {
private T[] stackarray;
private int top = -1;
private int size;
@SuppressWarnings("unchecked")
public StackClone(int size) {
stackarray = (T[])new Object[size];
this.size = size;
}
public void push(T input) {
if(top >= size) return;
stackarray[++top] = input;
}
public boolean isEmpty() {
if(top == -1) return true;
return false;
}
public T pop() {
if(isEmpty()) return null;
T save = stackarray[top];
stackarray[top--] = null;
return save;
}
public T top() {
if(isEmpty()) return null;
return stackarray[top];
}
public int size() {
return top+1;
}
public StackClone<T> clone(){
StackClone<T> clone = new StackClone<T>(size);
for(int i=0; i<=top; i++) {
clone.push(stackarray[i]);
}
return clone;
}
public static void main(String[] args) {
StackClone<Integer> cloned = new StackClone<>(10);
for(int i=0; i<10; i++) {
cloned.push(i+1);
}
StackClone<Integer> clone = cloned.clone();
for(int i=0; i<10; i++) {
System.out.println("clone: "+clone.pop());
}
System.out.println(cloned.isEmpty());
}
}