pub struct MinStack<T> { /* private fields */ }Expand description
A LIFO stack that supports O(1) push, pop and find-minimum.
Internally, each element on the stack contains a value and the index of the minimum value below it (or itself if it is smaller than everything below it). Each time we push, we compare the new value to the smallest value already on the stack. If it is smaller, we push the new value with its own index. Otherwise, we push the new value with the index of the smaller value that was already on the stack.
To find the minimum value, we just use the index of the last element on the stack.
§Example
use rust_dsa::MinStack;
// First, we create a new stack.
let mut stack = MinStack::new();
// We can push elements.
stack.push(2);
stack.push(3);
stack.push(6);
stack.push(1);
// We can get the minimum element.
assert_eq!(stack.get_min(), Some(&1));
// We can peek and pop as usual.
assert_eq!(stack.peek(), Some(&1));
assert_eq!(stack.pop(), Some(1));
// The min element reflects the stack's new state.
assert_eq!(stack.get_min(), Some(&2));
// We can iterate over the stack.
for x in stack {
// Prints 6, 3 and 2.
println!("{x}");
}
// We can also create stacks from arrays.
let a = MinStack::from(['s', 't', 'a', 'c', 'k']);
// And iterators.
let b: MinStack<_> = "stack".chars().collect();
assert!(a == b);Implementations§
Source§impl<T> MinStack<T>
impl<T> MinStack<T>
Sourcepub fn with_capacity(capacity: usize) -> MinStack<T>
pub fn with_capacity(capacity: usize) -> MinStack<T>
Creates an empty stack with the specified capacity.
Sourcepub fn push(&mut self, value: T)where
T: Ord,
pub fn push(&mut self, value: T)where
T: Ord,
Pushes an element onto the top of the stack.
§Example
use rust_dsa::MinStack;
let mut stack = MinStack::new();
stack.push(5);
assert_eq!(stack.pop(), Some(5));
assert_eq!(stack.pop(), None);Sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Removes the last element on the stack and returns it or None if the stack is empty.
§Example
use rust_dsa::MinStack;
let mut stack = MinStack::from([5]);
assert_eq!(stack.pop(), Some(5));
assert_eq!(stack.pop(), None);Sourcepub fn peek(&self) -> Option<&T>
pub fn peek(&self) -> Option<&T>
Returns a reference the last element on the stack or None if the stack is empty.
§Example
use rust_dsa::MinStack;
let mut stack = MinStack::from(['a']);
assert_eq!(stack.peek(), Some(&'a'));
stack.pop();
assert_eq!(stack.peek(), None);Sourcepub fn get_min(&mut self) -> Option<&T>
pub fn get_min(&mut self) -> Option<&T>
Returns the smallest element on the stack or None if the stack is empty.
§Example
use rust_dsa::MinStack;
let mut stack = MinStack::from([5, 3, 4, 8, 2, 6, 1]);
assert_eq!(stack.get_min(), Some(&1));
stack.pop();
assert_eq!(stack.get_min(), Some(&2));
stack.clear();
assert_eq!(stack.get_min(), None);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements on the stack.
§Example
use rust_dsa::MinStack;
let mut stack = MinStack::from([5, 3, 4, 8, 2, 6, 1]);
assert_eq!(stack.len(), 7);
stack.clear();
assert_eq!(stack.len(), 0);