A High-performance, Array based Stack implementation providing efficient LIFO (Last In, First Out) operations with fixed capacity and proper bounds checking.
A Stack is a linear data structure that follows the LIFO (Last In, First Out) principle. Elements are added and removed from the same end, called the "top" of the stack. Think of it like a stack of plates - you can only add or remove plates from the top.
- O(1) Performance: Constant time push, pop, and top operations
- Fixed Memory Usage: Array-based implementation with bounded capacity
- Memory Safe: Automatic cleanup prevents memory leaks
- Simple API: Clean, intuitive interface following standard conventions
- Robust Error Handling: Proper stack overflow and underflow protection
The following script installs Containers-Stack in Pharo.
Metacello new
baseline: 'ContainersStack';
repository: 'github://pharo-containers/Containers-Stack/src';
load.
Add the following code to your Metacello baseline or configuration
spec
baseline: 'ContainersStack'
with: [ spec repository: 'github://pharo-containers/Containers-Stack/src' ].
"Create a stack with capacity of 5"
stack := CTStack new: 5.
"Push elements"
stack push: 'first'.
stack push: 'second'.
stack push: 'third'.
"Check top element without removing"
stack top. "Returns 'third'"
stack size. "Returns 3"
"Pop elements (LIFO order)"
stack pop. "Returns 'third'"
stack pop. "Returns 'second'"
stack pop. "Returns 'first'"
"Stack is now empty"
stack isEmpty. "Returns true"
This is part of the Pharo Containers project. Feel free to contribute by implementing additional methods, improving tests, or enhancing documentation.