Singly-linked list/Element definition: Difference between revisions
Content deleted Content added
→{{header|D}}: make data public to make other examples easier |
m mass lang tag |
||
Line 3: | Line 3: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
<lang ada> type Link; |
|||
type Link_Access is access Link; |
type Link_Access is access Link; |
||
type Link is record |
type Link is record |
||
Next : Link_Access := null; |
Next : Link_Access := null; |
||
Data : Integer; |
Data : Integer; |
||
end record; |
end record;</lang> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Line 18: | Line 18: | ||
);</pre> |
);</pre> |
||
=={{header|C}}== |
=={{header|C}}== |
||
<lang c> struct link { |
|||
struct link *next; |
struct link *next; |
||
int data; |
int data; |
||
}; |
};</lang> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 27: | Line 27: | ||
The simplest C++ version looks basically like the C version: |
The simplest C++ version looks basically like the C version: |
||
<lang cpp> struct link |
|||
{ |
{ |
||
link* next; |
link* next; |
||
int data; |
int data; |
||
}; |
};</lang> |
||
Initialization of links on the heap can be simplified by adding a constructor: |
Initialization of links on the heap can be simplified by adding a constructor: |
||
<lang cpp> struct link |
|||
{ |
{ |
||
link* next; |
link* next; |
||
int data; |
int data; |
||
link(int a_data, link* a_next = 0): next(a_next), data(a_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: |
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). |
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; |
link* next; |
||
T data; |
T data; |
||
link(T a_data, link* a_next = 0): next(a_next), data(a_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. |
Note that the generic version works for any type, not only integral types. |
||
Line 65: | Line 65: | ||
Generic template-based node element. |
Generic template-based node element. |
||
<lang D> |
<lang D>class Node(T) { |
||
class Node(T) { |
|||
public: |
public: |
||
T data; |
T data; |
||
Node next; |
Node next; |
||
this(T d, Node n = null) { data=d; next=n; } |
this(T d, Node n = null) { data=d; next=n; } |
||
⚫ | |||
} |
|||
⚫ | |||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
Line 87: | Line 85: | ||
=={{header|E}}== |
=={{header|E}}== |
||
<lang e> interface LinkedList guards LinkedListStamp {} |
|||
def empty implements LinkedListStamp { |
def empty implements LinkedListStamp { |
||
to null() { return true } |
to null() { return true } |
||
Line 99: | Line 97: | ||
} |
} |
||
return link |
return link |
||
} |
}</lang> |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
Line 125: | Line 123: | ||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
In ISO Fortran 95 or later: |
In ISO Fortran 95 or later: |
||
type node |
<lang fortran> type node |
||
real :: data |
real :: data |
||
type( node ), pointer :: next => null() |
type( node ), pointer :: next => null() |
||
Line 132: | Line 130: | ||
!. . . . |
!. . . . |
||
! |
! |
||
type( node ) :: head |
type( node ) :: head</lang> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Line 140: | Line 138: | ||
An equivalent declaration for such a list type without the special syntax would look like this: |
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 |
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. |
but that would be really awkward to use. |
||
Line 152: | Line 150: | ||
The simplest Java version looks basically like the C++ version: |
The simplest Java version looks basically like the C++ version: |
||
<lang java> class Link |
|||
{ |
{ |
||
Link next; |
Link next; |
||
int data; |
int data; |
||
} |
}</lang> |
||
Initialization of links on the heap can be simplified by adding a constructor: |
Initialization of links on the heap can be simplified by adding a constructor: |
||
<lang java> class Link |
|||
{ |
{ |
||
Link next; |
Link next; |
||
int data; |
int data; |
||
Link(int a_data, Link a_next) { next = a_next; data = a_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: |
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+}} |
{{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). |
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; |
Link<T> next; |
||
T data; |
T data; |
||
Link(T a_data, Link<T> a_next) { next = a_next; data = a_data; } |
Link(T a_data, Link<T> a_next) { next = a_next; data = a_data; } |
||
} |
}</lang> |
||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
Line 201: | Line 199: | ||
An equivalent declaration for such a list type without the special syntax would look like this: |
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 |
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. |
but that would be really awkward to use. |
||
Line 213: | Line 211: | ||
However, if all you got is an algorithm in a foreign language, you can use references to accomplish the translation. |
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', |
data => 'say what', |
||
next => \%foo_node, |
next => \%foo_node, |
||
); |
); |
||
$node{next} = \%bar_node; # mutable |
$node{next} = \%bar_node; # mutable</lang> |
||
=={{header|Pop11}}== |
=={{header|Pop11}}== |
||
Line 296: | Line 294: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
class ListNode |
<lang ruby> class ListNode |
||
attr_accessor :value, :succ |
attr_accessor :value, :succ |
||
def initialize(v,s=nil) |
def initialize(v,s=nil) |
||
Line 307: | Line 305: | ||
end |
end |
||
include Enumerable |
include Enumerable |
||
end |
end</lang> |
||
=={{header|Scheme}}== |
=={{header|Scheme}}== |