LinkedStack<E>.from constructor

LinkedStack<E>.from(
  1. Iterable<E> iterable, [
  2. E? defaultValue
])

Creates a LinkedStack from an iterable. Elements are pushed in the order they appear in the iterable.

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.from(Iterable<E> iterable, [E? defaultValue]) : _defaultValue = defaultValue {
  for (final element in iterable) {
    push(element);
  }
}