-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathStackArray.java
160 lines (146 loc) · 4.23 KB
/
StackArray.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.thealgorithms.datastructures.stacks;
/**
* Implements a generic stack using an array.
*
* <p>This stack automatically resizes when necessary, growing to accommodate additional elements and
* shrinking to conserve memory when its size significantly decreases.
*
* <p>Elements are pushed and popped in LIFO (last-in, first-out) order, where the last element added
* is the first to be removed.
*
* @param <T> the type of elements in this stack
*/
public class StackArray<T> implements Stack<T> {
private static final int DEFAULT_CAPACITY = 10;
private int maxSize;
private T[] stackArray;
private int top;
/**
* Creates a stack with a default capacity.
*/
@SuppressWarnings("unchecked")
public StackArray() {
this(DEFAULT_CAPACITY);
}
/**
* Creates a stack with a specified initial capacity.
*
* @param size the initial capacity of the stack, must be greater than 0
* @throws IllegalArgumentException if size is less than or equal to 0
*/
@SuppressWarnings("unchecked")
public StackArray(int size) {
if (size <= 0) {
throw new IllegalArgumentException("Stack size must be greater than 0");
}
this.maxSize = size;
this.stackArray = (T[]) new Object[size];
this.top = -1;
}
/**
* Pushes an element onto the top of the stack. Resizes the stack if it is full.
*
* @param value the element to push
*/
@Override
public void push(T value) {
if (isFull()) {
resize(maxSize * 2);
}
stackArray[++top] = value;
}
/**
* Removes and returns the element from the top of the stack. Shrinks the stack if
* its size is below a quarter of its capacity, but not below the default capacity.
*
* @return the element removed from the top of the stack
* @throws IllegalStateException if the stack is empty
*/
@Override
public T pop() {
if (isEmpty()) {
throw new IllegalStateException("Stack is empty, cannot pop element");
}
T value = stackArray[top--];
if (top + 1 < maxSize / 4 && maxSize > DEFAULT_CAPACITY) {
resize(maxSize / 2);
}
return value;
}
/**
* Returns the element at the top of the stack without removing it.
*
* @return the top element of the stack
* @throws IllegalStateException if the stack is empty
*/
@Override
public T peek() {
if (isEmpty()) {
throw new IllegalStateException("Stack is empty, cannot peek element");
}
return stackArray[top];
}
/**
* Resizes the internal array to a new capacity.
*
* @param newSize the new size of the stack array
*/
private void resize(int newSize) {
@SuppressWarnings("unchecked") T[] newArray = (T[]) new Object[newSize];
System.arraycopy(stackArray, 0, newArray, 0, top + 1);
stackArray = newArray;
maxSize = newSize;
}
/**
* Checks if the stack is full.
*
* @return true if the stack is full, false otherwise
*/
public boolean isFull() {
return top + 1 == maxSize;
}
/**
* Checks if the stack is empty.
*
* @return true if the stack is empty, false otherwise
*/
@Override
public boolean isEmpty() {
return top == -1;
}
/**
* Empties the stack, marking it as empty without deleting elements. Elements are
* overwritten on subsequent pushes.
*/
@Override
public void makeEmpty() {
top = -1;
}
/**
* Returns the number of elements currently in the stack.
*
* @return the size of the stack
*/
@Override
public int size() {
return top + 1;
}
/**
* Returns a string representation of the stack.
*
* @return a string representation of the stack
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("StackArray [");
for (int i = 0; i <= top; i++) {
sb.append(stackArray[i]);
if (i < top) {
sb.append(", ");
}
}
sb.append("]");
return sb.toString();
}
}