LinkedStack<E> constructor
LinkedStack<E> ([
- E? defaultValue
Creates an empty LinkedStack.
A stack implementation using a linked list structure, mimicking Java's LinkedList as Stack.
This implementation provides efficient stack operations with constant-time push and pop operations. It uses a singly-linked list internally for optimal memory usage and performance.
Key Features:
- Constant-time operations: O(1) for push, pop, peek
- Memory efficient: Only allocates nodes as needed
- Unbounded: Can grow to any size (limited by available memory)
- LIFO ordering: Last element added is first to be removed
Usage Examples:
final stack = LinkedStack<String>();
// Push elements onto the stack
stack.push('bottom');
stack.push('middle');
stack.push('top');
// Process elements in LIFO order
while (!stack.isEmpty) {
print(stack.pop()); // top, middle, bottom
}
// Peek without removing
stack.push('peek-me');
print(stack.peek()); // 'peek-me'
print(stack.size()); // 1
Implementation
LinkedStack([E? defaultValue]) : _defaultValue = defaultValue;