Singly-linked list/Element definition: Difference between revisions

m
mass lang tag
(→‎{{header|D}}: make data public to make other examples easier)
m (mass lang tag)
Line 3:
=={{header|Ada}}==
 
<lang ada> type Link;
type Link_Access is access Link;
type Link is record
Next : Link_Access := null;
Data : Integer;
end record;</lang>
 
=={{header|ALGOL 68}}==
Line 18:
);</pre>
=={{header|C}}==
<lang c> struct link {
struct link *next;
int data;
};</lang>
 
=={{header|C++}}==
Line 27:
The simplest C++ version looks basically like the C version:
 
<lang cpp> struct link
{
link* next;
int data;
};</lang>
 
Initialization of links on the heap can be simplified by adding a constructor:
 
<lang cpp> struct link
{
link* next;
int data;
link(int a_data, link* a_next = 0): next(a_next), data(a_data) {}
};</lang>
 
With this constructor, new nodes can be initialized directly at allocation; e.g. the following code creates a complete list with just one statement:
 
<lang cpp> link* small_primes = new link(2, new link(3, new link(5, new link(7))));</lang>
 
However, C++ also allows to make it generic on the data type (e.g. if you need large numbers, you might want to use a larger type than int, e.g. long on 64-bit platforms, long long on compilers that support it, or even a bigint class).
 
<lang cpp> template<typename T> struct link
{
link* next;
T data;
link(T a_data, link* a_next = 0): next(a_next), data(a_data) {}
};</lang>
 
Note that the generic version works for any type, not only integral types.
Line 65:
Generic template-based node element.
 
<lang D>class Node(T) {
class Node(T) {
public:
T data;
Node next;
this(T d, Node n = null) { data=d; next=n; }
}</lang>
}
</lang>
 
=={{header|Delphi}}==
Line 87 ⟶ 85:
=={{header|E}}==
 
<lang e> interface LinkedList guards LinkedListStamp {}
def empty implements LinkedListStamp {
to null() { return true }
Line 99 ⟶ 97:
}
return link
}</lang>
 
=={{header|Forth}}==
Line 125 ⟶ 123:
=={{header|Fortran}}==
In ISO Fortran 95 or later:
<lang fortran> type node
real :: data
type( node ), pointer :: next => null()
Line 132 ⟶ 130:
!. . . .
!
type( node ) :: head</lang>
 
=={{header|Haskell}}==
Line 140 ⟶ 138:
An equivalent declaration for such a list type without the special syntax would look like this:
 
<lang haskell> data List a = Nil | Cons a (List a)</lang>
 
A declaration like the one required in the task, with an integer as element type and a mutable link, would be
 
<lang haskell> data IntList s = Nil | Cons Integer (STRef s (IntList s))</lang>
 
but that would be really awkward to use.
Line 152 ⟶ 150:
The simplest Java version looks basically like the C++ version:
 
<lang java> class Link
{
Link next;
int data;
}</lang>
 
Initialization of links on the heap can be simplified by adding a constructor:
 
<lang java> class Link
{
Link next;
int data;
Link(int a_data, Link a_next) { next = a_next; data = a_data; }
}</lang>
 
With this constructor, new nodes can be initialized directly at allocation; e.g. the following code creates a complete list with just one statement:
 
<lang java> Link small_primes = new Link(2, new Link(3, new Link(5, new Link(7, null))));</lang>
 
{{works with|Java|1.5+}}
However, Java also allows to make it generic on the data type. This will only work on reference types, not primitive types like int or float (wrapper classes like Integer and Float are available).
 
<lang java> class Link<T>
{
Link<T> next;
T data;
Link(T a_data, Link<T> a_next) { next = a_next; data = a_data; }
}</lang>
 
=={{header|Logo}}==
Line 201 ⟶ 199:
An equivalent declaration for such a list type without the special syntax would look like this:
 
<lang ocaml> type 'a list = Nil | Cons of 'a * 'a list</lang>
 
A declaration like the one required in the task, with an integer as element type and a mutable link, would be
 
<lang ocaml> type int_list = Nil | Cons of int * int_list ref</lang>
 
but that would be really awkward to use.
Line 213 ⟶ 211:
 
However, if all you got is an algorithm in a foreign language, you can use references to accomplish the translation.
<lang perl> my %node = (
data => 'say what',
next => \%foo_node,
);
$node{next} = \%bar_node; # mutable</lang>
 
=={{header|Pop11}}==
Line 296 ⟶ 294:
=={{header|Ruby}}==
 
<lang ruby> class ListNode
attr_accessor :value, :succ
def initialize(v,s=nil)
Line 307 ⟶ 305:
end
include Enumerable
end</lang>
 
=={{header|Scheme}}==