Singly-linked list/Element definition: Difference between revisions
Singly-linked list/Element definition (view source)
Revision as of 19:00, 14 March 2020
, 4 years agoRename Perl 6 -> Raku, alphabetize, minor clean-up
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 2:
{{Template:See also lists}}
=={{header|360 Assembly}}==
The program uses DSECT and USING pseudo instruction to define a node.
Line 97 ⟶ 98:
C
</pre>
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
Line 183 ⟶ 185:
}
}</lang>
=={{header|Ada}}==
Line 294 ⟶ 297:
bx lr @ return
</lang>
=={{header|AutoHotkey}}==
<lang AutoHotkey>element = 5 ; data
Line 367 ⟶ 371:
struct link *next;
int data;
};</lang>
=={{header|C sharp|C#}}==
<lang csharp>class LinkedListNode
{
public int Value { get; set; }
public LinkedListNode Next { get; set; }
// A constructor is not necessary, but could be useful.
public Link(int value, LinkedListNode next = null)
{
Item = value;
Next = next;
}
}</lang>
A generic version:
<lang csharp>class LinkedListNode<T>
{
public T Value { get; set; }
public LinkedListNode Next { get; set; }
public Link(T value, LinkedListNode next = null)
{
Item = value;
Next = next;
}
}</lang>
The most C-like possible version is basically C.
<lang csharp>unsafe struct link {
public link* next;
public int data;
};</lang>
Line 403 ⟶ 441:
Note that the generic version works for any type, not only integral types.
=={{header|
<lang clean>import StdMaybe
:: Link t = { next :: Maybe (Link t), data :: t }</lang>
=={{header|Clojure}}==
Line 449 ⟶ 458:
<lang lisp>(cons 1 (cons 2 (cons 3 nil)) => (1 2 3)</lang>
=={{header|D}}==
Line 495 ⟶ 499:
return link
}</lang>
=={{header|Elena}}==
<lang elena>class Link
Line 1,115 ⟶ 1,120:
);
$node{next} = \%bar_node; # mutable</lang>
=={{header|Phix}}==
Line 1,269 ⟶ 1,246:
(mcons 1 (mcons 2 (mcons 3 '()))) ; a mutable list
</lang>
=={{header|Raku}}==
(formerly Perl 6)
====With <tt>Pair</tt>====
A <tt>Pair</tt> (constructed with the <code>=></code> operator) can be treated as a cons cell, and thus used to build a linked lists:
<lang perl6>my $elem = 42 => $nextelem;</lang>
However, because this is not the primary purpose of the <tt>Pair</tt> type, it suffers from the following limitations:
* The naming of <tt>Pair</tt>'s accessor methods is not idiomatic for this use case (<code>.key</code> for the cell's value, and <code>.value</code> for the link to the next cell).
* A <tt>Pair</tt> (unlike an <tt>Array</tt>) 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:
<lang perl6>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));</lang>
=={{header|REXX}}==
Line 1,359 ⟶ 1,366:
list = ListNode.from_array([1,2,3,4])</lang>
=={{header|Run BASIC}}==
<lang runbasic>data = 10
link = 10
dim node{data,link} </lang>
=={{header|Rust}}==
Line 1,395 ⟶ 1,407:
// Do stuff
}</lang>
=={{header|Scala}}==
Line 1,443 ⟶ 1,450:
node{:next} = bar_node; # mutable</lang>
=={{header|SSEM}}==
|