Skip to main content

Queue

Struct Queue 

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

Halqasimon bufer (ring buffer) asosidagi navbat.

Nega Vec::remove(0) emas? U O(n) — hamma elementni chapga suradi. Bu yerda ikkita ko’rsatkich (head, len) bilan ishlaymiz va bufer oxiriga yetganda “boshiga o’ralib” ketamiz — natijada enqueue/dequeue O(1).

 bufer:  [ _ , C , D , E , _ ]
               ^head      ^ keyingi yozish joyi

Qayerda ishlatiladi: BFS, vazifalar navbati (task queue), printer navbati, tarmoq paketlari buferi.

§Misol

use rust_algorithms::data_structures::Queue;

let mut q = Queue::new();
q.enqueue("Ali");
q.enqueue("Vali");
assert_eq!(q.front(), Some(&"Ali"));
assert_eq!(q.dequeue(), Some("Ali"));
assert_eq!(q.dequeue(), Some("Vali"));
assert_eq!(q.dequeue(), None);

Implementations§

Source§

impl<T> Queue<T>

Source

pub fn new() -> Self

Bo’sh navbat yaratadi.

Source

pub fn enqueue(&mut self, item: T)

Oxiriga element qo’shadi. O(1) amortizatsiyalangan.

Source

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

Boshidan element oladi. O(1).

Source

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

Navbatning boshidagi elementga qaraydi.

Source

pub fn len(&self) -> usize

Elementlar soni.

Source

pub fn is_empty(&self) -> bool

Bo’shmi?

Trait Implementations§

Source§

impl<T: Debug> Debug for Queue<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for Queue<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> FromIterator<T> for Queue<T>

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Queue<T>

§

impl<T> RefUnwindSafe for Queue<T>
where T: RefUnwindSafe,

§

impl<T> Send for Queue<T>
where T: Send,

§

impl<T> Sync for Queue<T>
where T: Sync,

§

impl<T> Unpin for Queue<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Queue<T>

§

impl<T> UnwindSafe for Queue<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.