Singly-linked list/Element definition: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 2: | Line 2: | ||
{{Template:See also lists}} |
{{Template:See also lists}} |
||
=={{header|360 Assembly}}== |
=={{header|360 Assembly}}== |
||
The program uses DSECT and USING pseudo instruction to define a node. |
The program uses DSECT and USING pseudo instruction to define a node. |
||
Line 97: | Line 98: | ||
C |
C |
||
</pre> |
</pre> |
||
=={{header|AArch64 Assembly}}== |
=={{header|AArch64 Assembly}}== |
||
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
||
Line 183: | Line 185: | ||
} |
} |
||
}</lang> |
}</lang> |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
Line 294: | Line 297: | ||
bx lr @ return |
bx lr @ return |
||
</lang> |
</lang> |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
<lang AutoHotkey>element = 5 ; data |
<lang AutoHotkey>element = 5 ; data |
||
Line 367: | Line 371: | ||
struct link *next; |
struct link *next; |
||
int data; |
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> |
};</lang> |
||
Line 403: | Line 441: | ||
Note that the generic version works for any type, not only integral types. |
Note that the generic version works for any type, not only integral types. |
||
=={{header| |
=={{header|Clean}}== |
||
<lang clean>import StdMaybe |
|||
:: Link t = { next :: Maybe (Link t), data :: t }</lang> |
|||
<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> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Line 449: | Line 458: | ||
<lang lisp>(cons 1 (cons 2 (cons 3 nil)) => (1 2 3)</lang> |
<lang lisp>(cons 1 (cons 2 (cons 3 nil)) => (1 2 3)</lang> |
||
=={{header|Clean}}== |
|||
<lang clean>import StdMaybe |
|||
:: Link t = { next :: Maybe (Link t), data :: t }</lang> |
|||
=={{header|D}}== |
=={{header|D}}== |
||
Line 495: | Line 499: | ||
return link |
return link |
||
}</lang> |
}</lang> |
||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
<lang elena>class Link |
<lang elena>class Link |
||
Line 1,115: | Line 1,120: | ||
); |
); |
||
$node{next} = \%bar_node; # mutable</lang> |
$node{next} = \%bar_node; # mutable</lang> |
||
=={{header|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|Phix}}== |
=={{header|Phix}}== |
||
Line 1,269: | Line 1,246: | ||
(mcons 1 (mcons 2 (mcons 3 '()))) ; a mutable list |
(mcons 1 (mcons 2 (mcons 3 '()))) ; a mutable list |
||
</lang> |
</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}}== |
=={{header|REXX}}== |
||
Line 1,359: | Line 1,366: | ||
list = ListNode.from_array([1,2,3,4])</lang> |
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}}== |
=={{header|Rust}}== |
||
Line 1,395: | Line 1,407: | ||
// Do stuff |
// Do stuff |
||
}</lang> |
}</lang> |
||
=={{header|Run BASIC}}== |
|||
<lang runbasic>data = 10 |
|||
link = 10 |
|||
dim node{data,link} </lang> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
Line 1,443: | Line 1,450: | ||
node{:next} = bar_node; # mutable</lang> |
node{:next} = bar_node; # mutable</lang> |
||
=={{header|SSEM}}== |
=={{header|SSEM}}== |