Dynamic Stack Visualization
Explore stack operations with real-time visualization and step-by-step Java code execution. Watch how elements are pushed, popped, and peeked in a stack, following the LIFO (Last In, First Out) principle.
Stack is empty. Push elements to get started!
Size:
0
Capacity:
10
Last Operation:
None
Operation Explanation
Select an operation from the controls panel to see the step-by-step execution with highlighted code and detailed explanations.
Stack.java
View Source
public class Stack {
private Object[] stack;
private int size;
private int capacity;
private int top;
public Stack() {
this.capacity = 5;
this.stack = new Object[this.capacity];
this.size = 0;
this.top = -1;
}
private void resize() {
int newCapacity = capacity * 2;
this.stack = Arrays.copyOf(this.stack, newCapacity);
this.capacity = newCapacity;
}
public void push(T data) {
if (size == capacity) {
resize();
}
top++;
stack[top] = data;
size++;
}
public T pop() {
if (isEmpty()) {
return null;
}
T data = (T) stack[top];
stack[top] = null;
top--;
size--;
return data;
}
public T peek() {
if (isEmpty()) {
return null;
}
return (T) stack[top];
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
}
Stack Operations
Animation Controls
Slow
Fast
Presets
Statistics
Operations:
0
Comparisons:
0
Time Complexity (Last Operation):
-