-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path03-Stack.java
124 lines (101 loc) · 2.61 KB
/
03-Stack.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package basic.data.structures;
/**
* Stack implementation with an array.
* O(1) push, pop and top operations (Constant Amortized Time)
*
* @param <T> Type of the elements of the stack.
*/
public class Stack<T> {
private static final int DEFAULT_CAPACITY = 16;
private Object[] elements;
private int capacity;
private int size;
/**
* Create an empty stack.
*/
public Stack() {
elements = new Object[DEFAULT_CAPACITY];
capacity = DEFAULT_CAPACITY;
size = 0;
}
/**
* Add an element to the top of the stack.
*
* @param value Object to be added.
*/
public void push(T value) {
checkCapacity(size + 1);
elements[size++] = value;
}
/**
* Return the element at the top of the stack.
*
* @return Reference to the object.
*/
@SuppressWarnings("unchecked")
public T top() {
if (size == 0) {
throw new IllegalStateException("Stack is empty");
}
return (T) elements[size - 1];
}
/**
* Remove an element from the top of the stack.
*/
public void pop() {
if (size == 0) {
throw new IllegalStateException("Stack is empty");
}
size--;
}
/**
* Remove the top element from the stack.
*
* @return Reference to the removed object.
*/
public T poll() {
T result = top();
pop();
return result;
}
/**
* @return The current number of elements in the stack.
*/
public int size() {
return size;
}
/**
* @return True if the stack has no elements and false otherwise.
*/
public boolean empty() {
return size == 0;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < size; i++) {
sb.append(elements[i].toString()).append(", ");
}
if (size > 0) {
sb.delete(sb.length() - 2, sb.length());
}
sb.append("]");
return sb.toString();
}
private void checkCapacity(int newSize) {
if (newSize >= 2 * capacity / 3) {
resize(capacity * 2);
}
if (newSize <= capacity / 3 && capacity > DEFAULT_CAPACITY) {
resize(capacity / 2);
}
}
private void resize(int newCapacity) {
Object[] temp = new Object[size];
System.arraycopy(elements, 0, temp, 0, size);
capacity = newCapacity;
elements = new Object[capacity];
System.arraycopy(temp, 0, elements, 0, size);
}
}