Singly-linked list/Element removal: Difference between revisions

Line 226:
 
=={{header|ATS}}==
I repeated the [[Singly-linked_list/Element_definition#ATS|‘Rosetta Code linear list type’]] here, so you can simply copy
the code below, compile it, and run it.
 
Also I put the executable parts in initialization rather than the main program,
to avoid being forced to ‘consume’ the list (free its memory). I felt that would be a distraction.
 
The deletion routine below may be surprisingly involved. Keep in mind, however, that deleting a node means altering the node that comes ''before'' it. Thus one has to look ahead in the list, to see whether you have gotten to the right place. Also, unless your list structure includes a ‘phony’ first node (which is sometimes done), you have to handle a match with the first node specially.
 
The deletion routine presented here proves that the result is the same length or one node shorter than the original. Also the search is proven to terminate.
 
<lang ATS>(*------------------------------------------------------------------*)
 
Line 365 ⟶ 375:
main0 () = ()</lang>
 
{{out}}
<pre>$ patscc -DATS_MEMALLOC_LIBC singly_linked_list_deletion.dats && ./a.out
123
789</pre>
 
=={{header|C}}==
1,448

edits