Struct algorithms_rs::queue::Queue

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

Queue data structure

Implementations§

Create an empty queue of fixed size

use algorithms_rs::Queue;

let empty_queue = Queue::<i32>::new(1);

assert_eq!(empty_queue.is_empty(), true);

Determine if queue is empty

use algorithms_rs::Queue;

let empty_queue = Queue::<i32>::new(1);

assert_eq!(empty_queue.is_empty(), true);
Examples found in repository?
src/queue.rs (line 101)
100
101
102
103
104
105
106
107
108
109
110
111
    pub fn de_queue(&mut self) -> anyhow::Result<T> {
        if self.is_empty() {
            return Err(anyhow::anyhow!("underflow"));
        }
        let element = self.data.get(self.head);
        if self.head == (self.len - 1) {
            self.head = 0;
        } else {
            self.head += 1;
        }
        Ok(element.unwrap().clone())
    }

Enter the queue from the end of the queue

use algorithms_rs::Queue;

let mut queue = Queue::<i32>::new(3);

queue.en_queue(1);
queue.en_queue(2);

assert_eq!(queue.is_empty(), false);

From the head of the queue Out of the queue

use algorithms_rs::Queue;

let mut queue = Queue::<i32>::new(3);

queue.en_queue(1);
queue.en_queue(2);

queue.de_queue();
queue.de_queue();

assert_eq!(queue.is_empty(), true);

Trait Implementations§

Formats the value using the given formatter. 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.