Skip to main content

SinglyLinkedList

Struct SinglyLinkedList 

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

Bir tomonlama bog’langan ro’yxat.

Option<Box<Node<T>>> — Rustdagi eng tabiiy ko’rinish:

  • None — ro’yxat/quyruq tugadi (NULL ning xavfsiz muqobili);
  • Box — tugun heapda yashaydi, chunki uning o’lchami rekursiv.

§Misol

use rust_algorithms::linked_list::SinglyLinkedList;

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

list.reverse();
assert_eq!(list.to_vec(), vec![3, 2, 1]);
assert_eq!(list.pop_front(), Some(3));
assert_eq!(list.len(), 2);

Implementations§

Source§

impl<T> SinglyLinkedList<T>

Source

pub fn new() -> Self

Bo’sh ro’yxat.

Source

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

Boshiga qo’shadi — O(1), linked listning asosiy afzalligi.

Source

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

Boshidan oladi — O(1).

Source

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

Oxiriga qo’shadi — O(n), chunki oxirigacha yurish kerak.

Source

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

Oxirgi elementni oladi — O(n).

Source

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

Birinchi qiymatga qaraydi.

Source

pub fn get(&self, index: usize) -> Option<&T>

index -qiymat (0 dan boshlab) — O(n).

Source

pub fn reverse(&mut self)

Ro’yxatni joyida teskari o’giradi — O(n), qo’shimcha xotirasiz.

Klassik “uch ko’rsatkich” usuli: prev, curr, next. Har qadamda joriy tugunning strelkasini orqaga qaratamiz.

 boshida:  1 → 2 → 3 → ✕
 1-qadam:  ✕ ← 1   2 → 3 → ✕
 oxirida:  ✕ ← 1 ← 2 ← 3
Source

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

O’rtadagi element (ikkita “ko’rsatkich” usuli: tez va sekin).

G’oya: sekin ko’rsatkich 1 qadam, tez ko’rsatkich 2 qadam yuradi. Tez ko’rsatkich oxiriga yetganda sekini roppa-rosa o’rtada bo’ladi — ro’yxat uzunligini oldindan bilmasdan, bitta yurishda.

§Misol
use rust_algorithms::linked_list::SinglyLinkedList;

let list: SinglyLinkedList<i32> = (1..=5).collect();
assert_eq!(list.middle(), Some(&3));
Source

pub fn len(&self) -> usize

Elementlar soni — O(1) (hisoblab boriladi).

Source

pub fn is_empty(&self) -> bool

Bo’shmi?

Source

pub fn iter(&self) -> Iter<'_, T>

Boshidan oxirigacha iterator.

Source

pub fn to_vec(&self) -> Vec<T>
where T: Clone,

Vektorga aylantiradi (nusxa olmasdan — havolalar orqali yig’adi).

Source§

impl<T: PartialEq> SinglyLinkedList<T>

Source

pub fn contains(&self, value: &T) -> bool

Qiymat ro’yxatda bormi? — O(n).

Trait Implementations§

Source§

impl<T: Debug> Debug for SinglyLinkedList<T>

Source§

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

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

impl<T> Default for SinglyLinkedList<T>

Source§

fn default() -> Self

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

impl<T> Drop for SinglyLinkedList<T>

Rekursiv Drop stack overflow bermasligi uchun qo’lda bo’shatamiz.

Standart drop millionta tugunli ro’yxatda rekursiv chaqiriladi va stack tugab qoladi. Bu yerda tugunlarni tsikl bilan birma-bir ozod qilamiz.

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 SinglyLinkedList<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 SinglyLinkedList<T>

§

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

§

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

§

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

§

impl<T> Unpin for SinglyLinkedList<T>

§

impl<T> UnsafeUnpin for SinglyLinkedList<T>

§

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