Jump to content

Singly-linked list/Element definition: Difference between revisions

no edit summary
No edit summary
Line 250:
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.
Line 278:
yield cursor.value
cursor = cursor.next
</pythonlang>
 
'''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.
Line 299:
=={{header|Scheme}}==
 
Scheme, like other Lisp dialects, has extensive support for singly-linked lists. The element of such a list is known as a ''cons-pair'', because you use the <codett>cons</codett> function to construct it:
<lang scheme>(cons value next)</schemelang>
 
The value and next-link parts of the pair can be deconstructed using the <codett>car</codett> and <codett>cdr</codett> functions, respectively:
<lang scheme>(car my-list) ; returns the first element of the list
(cdr my-list) ; returns the remainder of the list</schemelang>
 
Each of these parts are mutable and can be set using the <codett>set-car!</codett> and <codett>set-cdr!</codett> functions, respectively:
<lang scheme>(set-car! my-list new-elem)
(set-cdr! my-list new-next)</schemelang>
Cookies help us deliver our services. By using our services, you agree to our use of cookies.