Anonymous user
Singly-linked list/Element definition: Difference between revisions
Singly-linked list/Element definition (view source)
Revision as of 01:57, 22 November 2009
, 14 years agoFixed lang tags.
m (→{{header|Ruby}}: update code) |
m (Fixed lang tags.) |
||
Line 3:
=={{header|Ada}}==
<lang ada>
=={{header|ALGOL 68}}==
<
MODE LINK = STRUCT (
REF LINK next,
DATA value
);</
=={{header|AutoHotkey}}==
<lang AutoHotkey>element = 5 ; data
Line 22:
=={{header|C}}==
<lang c>
=={{header|C++}}==
Line 31:
The simplest C++ version looks basically like the C version:
<lang cpp>
Initialization of links on the heap can be simplified by adding a constructor:
<lang cpp>
With this constructor, new nodes can be initialized directly at allocation; e.g. the following code creates a complete list with just one statement:
Line 52:
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>
Note that the generic version works for any type, not only integral types.
Line 75:
=={{header|Clean}}==
=={{header|D}}==
Line 93:
A simple one way list. I use a generic pointer for the data that way it can point to any structure, individual variable or whatever. Note that in Standard Pascal, there are no generic pointers, therefore one has to settle for a specific data type there.
<lang delphi>
=={{header|E}}==
<lang e>
=={{header|Forth}}==
Line 120:
Forth has no "struct" facility, but you can easily implement a single linked list with a data cell using a double-cell value.
As an example of usage, here is a word to link 'a' after 'b'
Or with named parameters:
Due to Forth's lack of typechecking, 'b' in the above examples does not have to be an actual cell, but can instead be the head pointer of the list.
Line 140:
=={{header|Fortran}}==
In ISO Fortran 95 or later:
<lang fortran>
=={{header|Haskell}}==
Line 165:
=={{header|J}}==
<lang J>list=: 0 2$0
This creates and then displays an empty list, with zero elements. The first number in an item is (supposed to be) the index of the next element of the list (_ for the final element of the list). The second number in an item is the numeric value stored in that list item. The list is named and names are mutable in J which means links are mutable.
Line 174 ⟶ 172:
To create such a list with one element which contains number 42, we can do the following:
<lang J> list=: ,: _ 42
list
_ 42</lang>
Now list contains one item, with index of the next item and value.
Line 184 ⟶ 180:
This solution employs the fact that data types for index and for node content are the same. If we need to store, for example, strings in the nodes, we should do something different, for example:
<lang J> list=: 0 2$a: NB. creates list with 0 items▼
▲ list=: 0 2$a: NB. creates list with 0 items
list
list=: ,: (<_) , <'some text' NB. creates list with 1 item
Line 191 ⟶ 186:
+-+---------+
|_|some text|
+-+---------+</lang>
=={{header|Java}}==
Line 198 ⟶ 192:
The simplest Java version looks basically like the C++ version:
<lang java>
Initialization of links on the heap can be simplified by adding a constructor:
<lang java>
With this constructor, new nodes can be initialized directly at allocation; e.g. the following code creates a complete list with just one statement:
Line 220 ⟶ 214:
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>
=={{header|JavaScript}}==
Line 262 ⟶ 256:
As with other list-based languages, simple lists are represented easily in Logo.
These return modified lists, but you cal also destructively modify lists. These are normally not used because you might accidentally create cycles in the list.
=={{header|Objective-C}}==
Line 338 ⟶ 332:
However, if all you got is an algorithm in a foreign language, you can use references to accomplish the translation.
<lang perl>
=={{header|Pop11}}==
Line 348 ⟶ 342:
List are built in into Pop11, so normally on would just use them:
If however one wants to definite equivalent user-defined type, one can do this:
=={{header|Python}}==
Line 387 ⟶ 381:
The Node class implements also iteration for more Pythonic iteration over linked lists.
<lang python>class LinkedList(object):
▲ """USELESS academic/classroom example of a linked list implemented in Python.
▲ Don't ever consider using something this crude! Use the built-in list() type!
▲ """
class Node(object):
def __init__(self, item):
Line 414 ⟶ 407:
while cursor:
yield cursor.value
cursor = cursor.next</lang>
'''Note:''' As explained in this class' docstring implementing linked lists and nodes in Python is an utterly pointless academic exercise. It may give on the flavor of the elements that would be necessary in some other programming languages (''e.g.'' using Python as "executable psuedo-code"). Adding methods for finding, counting, removing and inserting elements is left as an academic exercise to the reader. For any practical application use the built-in ''list()'' or ''dict()'' types as appropriate.
|