DSAVisualiseData Structures & Algorithms Studio
Lists & Stacks Hard

Array and Linked-List Stacks

Compare the source's linked-node and resizable-array stack implementations through push, peek, pop, and empty operations.

Trace input

Bounded JSON

Edit the lesson input, then build a deterministic source-linked trace.

This lesson validates its own input and operation limits.

Source scenarios

Complexity

Bestpush, peek, and pop O(1)
Averagepush O(1) amortized; peek and pop O(1)
Worstpush O(n) on growth; pop O(n) on resize
SpaceO(n) live items; ArrayStack may retain spare capacity

Array state

Follow the active comparisons and writes.

Live snapshot

Current operation

Trace state
Ready to explore
Source behavior & common mistakes

Follow stable item identities, top-of-stack access, EmptyStackException behavior, and the ArrayStack's exact grow and shrink rules.

  • The source's ArrayStack starts with capacity 10 and doubles only when size equals array.length.
  • ArrayStack pop decrements size, clears the vacated slot, then shrinks when size > 0 and size <= array.length / 2.
  • LinkedStack updates top before decrementing size when popping.
  • A null payload is valid; emptiness depends on size, not on the top value.
  • Both peek and pop throw EmptyStackException when size is zero.

Algorithm code

Java
Stack.javaStep linked
The highlighted line follows the current step.

Ready to begin

Press Play to see what happens.

Detailed execution
Snapshot 1 / 1