Comments in Korean is below.
References
Stack operations may involve initializing the stack, using it and then de-initializing it. Apart from these basic stuffs, a stack is used for the following two primary operations −
When data is PUSHed onto stack.
To use a stack efficiently, we need to check the status of stack as well. For the same purpose, the following functionality is added to stacks −
Implement using JAVA ✨ Source: datastructure/Stack.java
import lombok.extern.slf4j.Slf4j;
import java.util.Arrays;
@Slf4j
public class Stack<T> {
/* Field */
private final Object[] stackArray;
private final Integer maxSize;
private Integer top;
/* Constructor */
public Stack(final Integer stackSize) {
stackArray = new Object[stackSize];
maxSize = stackSize;
top = -1;
}
/* Push */
public void push(final T target) {
if (isFull()) {
log.error("Stack Overflow\\nIt is not able to PUSH.");
return;
}
stackArray[++top] = target;
}
/* Pop */
public T pop() {
if (isEmpty()) {
log.error("Stack is Empty...");
return null;
} else {
try {
return (T) stackArray[top];
} finally {
stackArray[top--] = null;
}
}
}
/* Peek */
public T peek() {
return (T) stackArray[top];
}
/* isFull */
public boolean isFull() {
return top == maxSize - 1;
}
/* isEmpty */
public boolean isEmpty() {
return top == -1;
}
@Override
public String toString() {
return "Stack{" +
"stackArray=" + Arrays.toString(stackArray) +
'}';
}
}