Skip to main content

DoublyLinkedList

Struct DoublyLinkedList 

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

Ikki tomonlama bog’langan ro’yxat: har bir tugun oldingi va keyingisini biladi.

 ✕ ⇄ [10] ⇄ [20] ⇄ [30] ⇄ ✕
      ^head            ^tail

Ikkala uchdan ham qo’shish/olish O(1) — shuning uchun u deque sifatida ishlaydi.

§Rust darsi: Rc, RefCell, Weak

  • Rc<T> — bitta qiymatga bir nechta egalik (havolalar sanoqchisi bilan);
  • RefCell<T> — o’zgartirishni ish vaqtida tekshirish (Rc ichida &mut olib bo’lmaydi, RefCell shu cheklovni “ichkariga” ko’chiradi);
  • Weak<T> — sanoqchini oshirmaydigan havola; sikllarni oldini oladi.

Bu narx bilan keladi: har murojaatda borrow() tekshiruvi. Shuning uchun Rustda odatda VecDeque afzal.

§Misol

use rust_algorithms::linked_list::DoublyLinkedList;

let mut l = DoublyLinkedList::new();
l.push_back(2);
l.push_back(3);
l.push_front(1);
assert_eq!(l.to_vec(), vec![1, 2, 3]);
assert_eq!(l.to_vec_rev(), vec![3, 2, 1]);

assert_eq!(l.pop_back(), Some(3));
assert_eq!(l.pop_front(), Some(1));
assert_eq!(l.len(), 1);

Implementations§

Source§

impl<T> DoublyLinkedList<T>

Source

pub fn new() -> Self

Bo’sh ro’yxat.

Source

pub fn push_front(&mut self, value: T)

Boshiga qo’shadi — O(1).

Source

pub fn push_back(&mut self, value: T)

Oxiriga qo’shadi — O(1) (bir tomonlama ro’yxatdan farqi shu!).

Source

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

Boshidan oladi — O(1).

Source

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

Oxiridan oladi — O(1).

Source

pub fn len(&self) -> usize

Elementlar soni.

Source

pub fn is_empty(&self) -> bool

Bo’shmi?

Source§

impl<T: Clone> DoublyLinkedList<T>

Source

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

Birinchi qiymatning nusxasi.

Source

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

Oxirgi qiymatning nusxasi.

Source

pub fn to_vec(&self) -> Vec<T>

Boshidan oxirigacha vektor.

Source

pub fn to_vec_rev(&self) -> Vec<T>

Oxiridan boshigacha vektor — prev havolalari ishlayotganini ko’rsatadi.

Trait Implementations§

Source§

impl<T> Default for DoublyLinkedList<T>

Source§

fn default() -> Self

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

impl<T> Drop for DoublyLinkedList<T>

Rekursiv Drop dan qochish uchun tugunlarni tsiklda bo’shatamiz.

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<T> FromIterator<T> for DoublyLinkedList<T>

Source§

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

Creates a value from an iterator. Read more

Auto Trait Implementations§

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.