Struct algorithms_rs::stack::Stack

source ·
pub struct Stack<T> { /* private fields */ }
Expand description

stack data structure

在栈中,被删除的是最近插入的元素: 栈的实现是一种后进先出策略。 这里采用数组实现栈,还有别的方式也是可以实现栈的。

栈的操作

栈的操作有,栈上的INSERT操作称为压入PUSH,而无元素参数的DELETE操作称为弹出POP。

Implementations§

Creating an empty stack

use algorithms_rs::Stack;

let stack = Stack::<i32>::new();

assert_eq!(stack.is_empty(), true);
Examples found in repository?
src/stack.rs (line 19)
18
19
20
    fn default() -> Self {
        Self::new()
    }

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
Examples found in repository?
src/stack.rs (line 112)
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
    pub fn pop(&mut self) -> anyhow::Result<T> {
        if self.is_empty() {
            Err(anyhow::anyhow!("underflow"))
        } else {
            self.top -= 1;
            Ok(self.data.remove(self.top))
        }
    }

    /// Return the top element of the stack
    ///
    /// ```rust
    /// use algorithms_rs::Stack;
    ///
    /// let mut stack = Stack::<i32>::new();
    ///
    /// assert_eq!(stack.peek(), None);
    ///
    /// stack.push(1);
    /// stack.push(2);
    ///
    /// assert_eq!(stack.peek(), Some(&2));
    /// assert_eq!(stack.is_empty(), false);
    /// ```
    pub fn peek(&self) -> Option<&T> {
        if self.is_empty() {
            return None;
        }
        self.data.get(self.top - 1)
    }

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

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]

Return the top element of the stack

use algorithms_rs::Stack;

let mut stack = Stack::<i32>::new();

assert_eq!(stack.peek(), None);

stack.push(1);
stack.push(2);

assert_eq!(stack.peek(), Some(&2));
assert_eq!(stack.is_empty(), false);

the stack size

use algorithms_rs::Stack;

let stack = Stack::<i32>::new();

assert_eq!(0, stack.size());

Trait Implementations§

Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.