Anonymous user
Singly-linked list/Element insertion: Difference between revisions
Singly-linked list/Element insertion (view source)
Revision as of 23:46, 29 September 2018
, 5 years ago→{{header|X86 Assembly}}
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Re-added removrd language tags. Re-marked as incorrect) |
|||
Line 1,449:
See [[Singly-linked list/Element definition#Stata]].
=={{header|Tcl}}==
This task is extremely against the nature of the Tool Command Language. There are built-in lists, which are first-class citizens. The command <tt>linsert</tt> for inserting in such a list is already there, but it returns a new list instead of modifying an existing one. To emulate this, the <i>name</i> of the list (instead of its value) has to be handed over to the procedure and the procedure has to be given access to the variable using the <tt>upvar</tt> construction.
Additionally, the inserting point is usually given by the <i>index</i> of the element, which is to <i>follow</i> the new element, so the insertion always happens <i>before</i>. Since references and pointers don't exist in Tcl, using an existing element (which can only be a value) to determine the position of insertion, is not a good idea, because any value may appear several times in the list.
No error checking is included.
<lang tcl>
proc insertIntoList {existingList predecessor newElement} {
upvar $existingList exList
set exList [linsert $exList [expr [lsearch -exact $exList $predecessor] + 1] $newElement]
}
set list {A B}
insertIntoList list A C
puts $list
</lang>
{{out}}
<pre>
A C B
</pre>
=={{header|X86 Assembly}}==
|