-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayStack.java
99 lines (81 loc) · 2.08 KB
/
ArrayStack.java
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
interface Stack<E> {
int size();
boolean isEmpty();
void push(E e);
E top();
E pop();
void getData();
void reverse();
}
public class ArrayStack<E> implements Stack<E> {
public static final int CAPACITY = 5;
private E[] data;
private int t = -1;
public ArrayStack() {
this(CAPACITY);
}
@SuppressWarnings("unchecked")
public ArrayStack(int capacity) {
data = (E[]) new Object[capacity];
}
public int size() {
return (t + 1);
}
public void push(E e) throws IllegalStateException {
if (size() == data.length) {
throw new IllegalStateException("Stack is full!");
}
data[++t] = e;
}
public E top() {
if (isEmpty()) {
return null;
}
return data[t];
}
public E pop() {
if (isEmpty()) {
return null;
}
E answer = data[t];
data[t] = null;
t--;
return answer;
}
public boolean isEmpty() {
return size() == 0;
}
public void reverse() {
Stack<E> buffer = new ArrayStack<>();
for (int i = 0; i < data.length; i++) {
buffer.push(data[i]);
// System.out.println(data[i]);
}
for (int i = 0; i < data.length; i++) {
data[i] = buffer.pop();
}
}
public void getData() {
for (int i = 0; i < size(); i++) {
System.out.print(data[i] + " ");
}
}
public static void main(String[] args) {
// Integer[] myArr = { 5, 4, 3, 2, 1 };
// Integer[] myArr1 = { 1, 2, 3, 4, 5 };
Stack<Integer> S = new ArrayStack<>();
S.push(5);
S.push(4);
S.push(3);
S.push(2);
S.push(1);
System.out.println("Size is: " + S.size());
System.out.println("Top is: " + S.top());
System.out.println(S.pop());
System.out.println("After pop Top is: " + S.top());
S.getData();
System.out.println("\nAfter reverse: ");
S.reverse();
S.getData();
}
}