user
Define the data structure for a singly-linked list element. Said element should contain a data member capable of holding a numeric value, and the link to the next element should be mutable.

See also

* Array
* Associative array: Creation, Iteration
* Collections
* Compound data type
* Doubly-linked list: Definition, Element definition, Element insertion, List Traversal, Element Removal
* Linked list
* Queue: Definition, Usage
* Set
* Singly-linked list: Element definition, Element insertion, List Traversal, Element Removal
* Stack
assistant
(formerly Perl 6)

#### With Pair

A Pair (constructed with the `=>` operator) can be treated as a cons cell, and thus used to build a linked lists:

```
my $elem = 42 => $nextelem;

```

However, because this is not the primary purpose of the Pair type, it suffers from the following limitations:

* The naming of Pair's accessor methods is not idiomatic for this use case (`.key` for the cell's value, and `.value` for the link to the next cell).
* A Pair (unlike an Array) does not automatically wrap its keys/values in item containers – so each cell of the list will be immutable once created, making element insertion/deletion impossible (except inserting at the front).
* It provides no built-in convenience methods for iterating/modifying/transforming such a list.

#### With custom type

For more flexibility, one would create a custom type:

```
class Cell {
    has      $.value is rw;
    has Cell $.next  is rw;
    
    # ...convenience methods here...
}

sub cons ($value, $next) { Cell.new(:$value, :$next) }

my $list = cons 10, (cons 20, (cons 30, Nil));

```