Anonymous user
Singly-linked list/Element definition: Difference between revisions
Singly-linked list/Element definition (view source)
Revision as of 17:35, 7 April 2009
, 15 years agomass lang tag
(→{{header|D}}: make data public to make other examples easier) |
m (mass lang tag) |
||
Line 3:
=={{header|Ada}}==
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}}==
struct link *next;
int data;
};</lang>
=={{header|C++}}==
Line 27:
The simplest C++ version looks basically like the C version:
{
link* next;
int data;
};</lang>
Initialization of links on the heap can be simplified by adding a constructor:
{
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:
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).
{
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) {
public:
T data;
Node next;
this(T d, Node n = null) { data=d; next=n; }
}</lang>▼
▲</lang>
=={{header|Delphi}}==
Line 87 ⟶ 85:
=={{header|E}}==
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:
A declaration like the one required in the task, with an integer as element type and a mutable link, would be
but that would be really awkward to use.
Line 152 ⟶ 150:
The simplest Java version looks basically like the C++ version:
{
Link next;
int data;
}</lang>
Initialization of links on the heap can be simplified by adding a constructor:
{
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:
{{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).
{
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:
A declaration like the one required in the task, with an integer as element type and a mutable link, would be
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.
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}}==
|