Struct Stack

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

A stack data structure.

This structure implements a simple stack where elements of type T are pushed and popped according to the Last-In-First-Out (LIFO) principle. The stack has a fixed size of 100 elements.

Implementations§

Source§

impl<T> Stack<T>

Source

pub fn new() -> Self

Creates a new, empty Stack.

§Examples
use dsa::data_structures::stack::Stack; 
let stack: Stack<i32> = Stack::new();
assert!(stack.is_empty());
Source

pub fn push(&mut self, value: T) -> Result<(), &str>

Pushes a value onto the Stack.

§Examples
use dsa::data_structures::stack::Stack;
let mut stack = Stack::new();
stack.push(10).unwrap();
stack.push(20).unwrap();
assert_eq!(stack.peek(), Some(&20));
assert_eq!(stack.size(), 2);
§Parameters
  • value: A T generic value that will be pushed onto the top of this Stack
§Returns
  • A Result object that return an Err if this Stack is full, otherwise an empty Ok(())
Source

pub fn pop(&mut self) -> Option<T>

Pops a value from the Stack.

§Examples
 use dsa::data_structures::stack::Stack;
 let mut stack: Stack<i32> = Stack::new();
 stack.push(10).unwrap();
 stack.push(20).unwrap();
 stack.pop();
 assert_eq!(stack.pop(), Some(10));
§Returns
  • An optional T generic that represents the topmost value that has been removed from this Stack
Source

pub fn peek(&self) -> Option<&T>

Returns a reference to the top element without removing it.

§Examples
 use dsa::data_structures::stack::Stack;
 let mut stack: Stack<i32> = Stack::new();
 stack.push(10).unwrap();
 stack.push(20).unwrap();
 stack.pop();
 assert_eq!(stack.peek(), Some(&10));
§Returns
  • The topmost value of this Stack as an optional T generic reference
Source

pub fn is_empty(&self) -> bool

Checks if the Stack is empty.

§Examples
use dsa::data_structures::stack::Stack;
 
let mut stack: Stack<u32> = Stack::new();
assert!(stack.is_empty());
§Returns
  • A bool value determining whether this Stack is empty
Source

pub fn size(&self) -> isize

Get the size of this Stack

§Examples
use dsa::data_structures::stack::Stack;
let mut stack: Stack<i32> = Stack::new();
stack.push(10).unwrap(); 
stack.push(20).unwrap();
stack.pop();
assert_eq!(stack.size(), 1); 
§Returns
  • An unsigned CPU architecture int representing the size of this Stack

Auto Trait Implementations§

§

impl<T> Freeze for Stack<T>
where T: Freeze,

§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.