Skip to main content
Log in

Mojo struct

LinkedList

struct LinkedList[ElementType: WritableCollectionElement]

A doubly-linked list implementation.

A doubly-linked list is a data structure where each element points to both the next and previous elements, allowing for efficient insertion and deletion at any position.

Parameters

  • ElementType (WritableCollectionElement): The type of elements stored in the list. Must implement WritableCollectionElement.

Implemented traits

AnyType, Sized, UnknownDestructibility, Writable

Methods

__init__

__init__(out self)

Initialize an empty linked list.

__init__(out self, owned *elements: ElementType)

Initialize a linked list with the given elements.

Args:

  • *elements (ElementType): Variable number of elements to initialize the list with.

__init__(out self, *, owned elements: VariadicListMem[ElementType, origin])

Initialize a linked list with the given elements.

Args:

  • elements (VariadicListMem[ElementType, origin]): Variable number of elements to initialize the list with.

__copyinit__

__copyinit__(out self, other: Self)

Initialize this list as a copy of another list.

Args:

  • other (Self): The list to copy from.

__moveinit__

__moveinit__(out self, owned other: Self)

Initialize this list by moving elements from another list.

Args:

  • other (Self): The list to move elements from.

__del__

__del__(owned self)

Clean up the list by freeing all nodes.

__bool__

__bool__(self) -> Bool

Check if the list is non-empty.

Returns:

True if the list has elements, False otherwise.

__getitem__

__getitem__(ref self, index: Int) -> ref [self] ElementType

Get the element at the specified index.

Args:

  • index (Int): The index of the element to get.

Returns:

The element at the specified index.

__setitem__

__setitem__(mut self, index: Int, owned value: ElementType)

Set the element at the specified index.

Args:

  • index (Int): The index of the element to set.
  • value (ElementType): The new value to set.

append

append(mut self, owned value: ElementType)

Add an element to the end of the list.

Args:

  • value (ElementType): The value to append.

prepend

prepend(mut self, owned value: ElementType)

Add an element to the beginning of the list.

Args:

  • value (ElementType): The value to prepend.

reverse

reverse(mut self)

Reverse the order of elements in the list.

pop

pop(mut self) -> ElementType

Remove and return the first element of the list.

Returns:

The first element in the list.

copy

copy(self) -> Self

Create a deep copy of the list.

Returns:

A new list containing copies of all elements.

__len__

__len__(self) -> Int

Get the number of elements in the list.

Returns:

The number of elements in the list.

__str__

__str__(self) -> String

Convert the list to its string representation.

Returns:

String representation of the list.

__repr__

__repr__(self) -> String

Convert the list to its string representation.

Returns:

String representation of the list.

write_to

write_to[W: Writer](self, mut writer: W)

Write the list to the given writer.

Parameters:

  • W (Writer): The type of writer to write the list to.

Args:

  • writer (W): The writer to write the list to.