pub struct Stack<T> { /* private fields */ }
Expand description
Implementations§
Source§impl<T: Clone> Stack<T>
impl<T: Clone> Stack<T>
Sourcepub fn new() -> Stack<T>
pub fn new() -> Stack<T>
Creating an empty stack
use algorithms_rs::Stack;
let stack = Stack::<i32>::new();
assert_eq!(stack.is_empty(), true);
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Determine if stack is empty
use algorithms_rs::Stack;
let mut stack = Stack::<i32>::new();
assert_eq!(stack.is_empty(), true);
stack.push(2);
assert_eq!(stack.is_empty(), false);
STACK-EMPTY(S)
if S.top == 0
return true
else return false
Sourcepub fn push(&mut self, element: T)
pub fn push(&mut self, element: T)
Put an element into the top of the stack of stack
use algorithms_rs::Stack;
let mut stack = Stack::<i32>::new();
stack.push(1);
assert_eq!(stack.is_empty(), false);
PUSH(S, x)
S.top = S.top + 1
S[S.top] = x
Sourcepub fn pop(&mut self) -> Result<T>
pub fn pop(&mut self) -> Result<T>
Remove an element from the top of the stack of stack
use algorithms_rs::Stack;
let mut stack = Stack::<i32>::new();
stack.push(1);
let element = stack.pop().unwrap();
assert_eq!(element, 1);
assert_eq!(stack.is_empty(), true);
if let Err(err) = stack.pop() {
assert_eq!(err.to_string(), "underflow".to_string())
}
POP(S)
if STACK-EMPTY(S)
error "underflow"
else
S.top = S.top - 1
return S[S.top + 1]
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for Stack<T>
impl<T> RefUnwindSafe for Stack<T>where
T: RefUnwindSafe,
impl<T> Send for Stack<T>where
T: Send,
impl<T> Sync for Stack<T>where
T: Sync,
impl<T> Unpin for Stack<T>where
T: Unpin,
impl<T> UnwindSafe for Stack<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more