Singly-linked list/Element removal: Difference between revisions

m
(C++ entry)
m (→‎{{header|Wren}}: Minor tidy)
 
(11 intermediate revisions by 5 users not shown)
Line 14:
The user must type in the monitor the following command after compilation and before running the program!<pre>SET EndProg=*</pre>
{{libheader|Action! Tool Kit}}
<langsyntaxhighlight Actionlang="action!">CARD EndProg ;required for ALLOCATE.ACT
 
INCLUDE "D2:ALLOCATE.ACT" ;from the Action! Tool Kit. You must type 'SET EndProg=*' from the monitor after compiling, but before running this program!
Line 125:
TestRemove(p)
TestRemove(listBegin)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Singly-linked_list_element_removal.png Screenshot from Atari 8-bit computer]
Line 150:
Using the STRINGLIST from the Singly-linked list Element traversal task.
{{Trans|ALGOL_W}}
<langsyntaxhighlight lang="algol68"># removes the specified element from the list, modifying list if necessary #
PRIO REMOVE = 1;
OP REMOVE = ( REF STRINGLIST list, REF STRINGLIST element )REF STRINGLIST:
Line 189:
( list REMOVE next OF next OF list ) REMOVE list;
 
</syntaxhighlight>
</lang>
 
=={{header|ALGOL W}}==
Uses the ListI record from the Singly Linked List task.
<langsyntaxhighlight lang="algolw">
% deletes the specified element from the list %
% if the element to remove is null or not in the list, %
Line 223:
Remove( head, head );
 
</syntaxhighlight>
</lang>
 
=={{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.
 
<syntaxhighlight lang="ats">(*------------------------------------------------------------------*)
 
(* The Rosetta Code linear list type can contain any vt@ype.
(The ‘@’ means it doesn’t have to be the size of a pointer.
You can read {0 <= n} as ‘for all non-negative n’. *)
dataviewtype rclist_vt (vt : vt@ype+, n : int) =
| rclist_vt_nil (vt, 0)
| {0 <= n} rclist_vt_cons (vt, n + 1) of (vt, rclist_vt (vt, n))
 
(* A lemma one will need: lists never have negative lengths. *)
extern prfun {vt : vt@ype}
lemma_rclist_vt_param
{n : int}
(lst : !rclist_vt (vt, n)) :<prf> [0 <= n] void
 
(* Proof of the lemma. *)
primplement {vt}
lemma_rclist_vt_param lst =
case+ lst of
| rclist_vt_nil () => ()
| rclist_vt_cons _ => ()
 
(*------------------------------------------------------------------*)
 
(* For simplicity, the Rosetta Code linear list deletion routine will
be specifically for lists of ‘int’. *)
 
(* Some things that will be needed. *)
#include "share/atspre_staload.hats"
 
(* The list is passed by reference and will be overwritten with
the new list. It will be proven that the new list is either
equal in length to the original or exactly one node shorter
than it.
(One might notice that in type expressions the equals sign
is ‘==’, even though in executable code it is ‘=’. There are
two distinct sublanguages; to my knowledge, this is merely how
the operators happen to be assigned in each sublanguage, within
the compiler’s prelude.) *)
extern fun
rclist_int_delete
{m : int} (* ‘for all list lengths m’ *)
(lst : &rclist_vt (int, m) >> (* & = pass by reference *)
(* The new type will be a list of the same
length or one less. *)
[n : int | n == m || (m <> 0 && n == m - 1)]
rclist_vt (int, n),
x : int) : void
 
(* The implementation is rather involved, and it will help to have
some convenient notation. The :: operator is already declared
in the compiler’s prelude as a right-associative infix operator. *)
#define NIL rclist_vt_nil ()
#define :: rclist_vt_cons
 
implement
rclist_int_delete {m} (lst, x) =
let
(* A recursive nested function that finds and deletes the node. *)
fun
find {k : int | 1 <= k}
.<k>. (* Means: ‘k must uniformly decrease towards zero.’
If so, that is proof that ‘find’ terminates. *)
(lst : &rclist_vt (int, k) >>
[j : int | j == k || j == k - 1]
rclist_vt (int, j),
x : int) : void =
case+ lst of
| (_ :: NIL) => () (* x was not found. Do nothing. *)
| (_ :: v :: _) when v = x =>
{
val+ @ (u :: tl) = lst (* @ = unfold. tl will be mutable. *)
val+ ~ (v :: tail) = tl (* ~ = consume. The v node will be
freed back into the heap. *)
val () = (tl := tail) (* Replace the u node’s tail with the
shortened tail. *)
prval () = fold@ lst (* Refold. Using ‘prval’ rather than
‘val’ means this statement applies
only to the typechecking phase. *)
}
| (_ :: _ :: _) =>
{
val+ @ (_ :: tl) = lst (* Unfold, so tl will be
referenceable. *)
val () = find (tl, x) (* Loop by tail recursion. *)
prval () = fold@ lst (* Refold. Using ‘prval’ rather than
‘val’ means this statement applies
only to the typechecking phase. *)
}
 
(* The following is needed to prove that lst does not have a
negative length. *)
prval _ = lemma_rclist_vt_param lst
in
case+ lst of
| NIL => () (* An empty list. Do nothing. *)
(* In the following, the ‘~’ means that the v node is consumed.
It will be freed back into the heap. The type notation
‘(v : int)’ is because (perhaps due to an overload of the
‘=’ operator) the typechecker had difficulty determining the
type of v. *)
| ~(v :: tail) when (v : int) = x =>
(* The first element matches. Replace the list with its tail. *)
lst := tail
| (_ :: _) => find (lst, x) (* Search in the list. *)
end
 
(* Now let’s try it. *)
 
overload delete with rclist_int_delete
 
val A = 123
val B = 789
val C = 456
 
(* ‘var’ instead of ‘val’, to make lst a mutable variable that can be
passed by reference. *)
var lst = A :: C :: B :: NIL
 
val () = delete (lst, C)
 
fun
loop {k : int | 0 <= k} .<k>.
(p : !rclist_vt (int, k)) : void =
case+ p of
| NIL => ()
| head :: tail =>
begin
println! (head);
loop tail
end
prval () = lemma_rclist_vt_param lst
val () = loop lst
 
(*------------------------------------------------------------------*)
 
implement
main0 () = ()</syntaxhighlight>
 
{{out}}
<pre>$ patscc -DATS_MEMALLOC_LIBC singly_linked_list_deletion.dats && ./a.out
123
789</pre>
 
''Afterword''. The Rosetta Code linear list type was a good exercise. The linear lists code in the ATS2 prelude contains many unsafe operations, which is fine for a well tested library, but not terribly enlightening. In contrast, I wrote the Rosetta Code linear list type using only ‘safe’ ATS2 code.
 
'''THE PURPOSE OF COMPUTING IS INSIGHT, NOT NUMBERS'''
 
=={{header|C}}==
This implementation takes up integers from the command line and then asks which element has to be removed. List is printed before and after removal, usage printed on incorrect invocation.
<syntaxhighlight lang="c">
<lang C>
#include<stdlib.h>
#include<stdio.h>
Line 327 ⟶ 486:
return 0;
}
</syntaxhighlight>
</lang>
Invocation, interaction and output :
<pre>
Line 344 ⟶ 503:
Semantically identical translation of Torvalds' tasteful C version using C# unsafe pointers:
 
<langsyntaxhighlight lang="csharp">using System;
using System.Runtime.InteropServices;
 
Line 395 ⟶ 554:
Console.WriteLine("after removing second node: " + head->ToString());
}
}</langsyntaxhighlight>
 
{{out}}
Line 404 ⟶ 563:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
 
// define a singly linked list
Line 465 ⟶ 624:
PrintList(head);
if (!removed2) std::cout << "\nItem not found\n";
}</langsyntaxhighlight>
{{out}}
<pre>
Line 474 ⟶ 633:
Item not found</pre>
 
=={{header|F_Sharp|F#}}==
Not really a functional thing to do but...
<syntaxhighlight lang="fsharp">
// Singly-linked list/Element removal. Nigel Galloway: March 22nd., 2022
let N=[23;42;1;13;0]
let fG n g=List.indexed n|>List.filter(fun(n,_)->n<>g)|>List.map snd
printfn " before: %A\nand after: %A" N (fG N 2)
</syntaxhighlight>
{{out}}
<pre>
before: [23; 42; 1; 13; 0]
and after: [23; 42; 13; 0]
</pre>
=={{header|Fortran}}==
This sort of thing has long been done in Fortran via the standard trick of fiddling with arrays, and using array indices as the equivalent of the memory addresses of nodes. The task makes no mention of there being any content associated with the links of the linked-list; this would be supplied via auxiliary arrays or disc file records, ''etc''. With F90 and later, one can define compound data aggregates, so something like LL.NEXT would hold the link to the next element and LL.STUFF would hold the cargo, with LL being an array of such a compound entity rather than separate simple arrays such as LLNEXT and LLSTUFF.
Line 481 ⟶ 653:
For convenience, rather than have the "head" pointer to the first or head element of the linked list be a separate variable, it is found as element zero of the LIST array that holds the links. Because this element is accessible in the same way as the other links in the array representing the linked-list, no special code is needed when it is the head entry that is to be removed and thus it is the pointer to it that must be changed. However, defining arrays starting at index zero is a feature of F90, and having subroutines recognise that their array parameter starts at index zero requires the MODULE protocol. Previously, arrays started with index one, and the code would just have to recognise this with the appropriate offsets, thus, the first element available for an item would be at index two, not one, and so forth. On the other hand, the zero element just needs its link, and any associated cargo would represented wasted storage. If that cargo were to be held in a disc file there would be no such waste, as record numbers start with one, not zero. But, if the linked-list is to be stored entirely in a disc file, the first record has to be reserved to hold the link to the head record and the first available storage record is number two, just as with an array starting at one, not zero. Indeed, a more comprehensive solution would probably reserve the first record as a header, containing a finger to the start of the linked-list, another finger to the start of the "available" (i.e. deleted and thus reusable) linked-list of records, and a record counter to identify the last record in the file so that if the "available" list is empty, the file can be extended by one record to hold a new entry.
 
Having a value of zero signify that there is no follower is the obvious choice for ending a linked-list. When addresses are being tossed about, this might be dressed up via words such as NULL rather than a literal zero just in case a "null address" does not manifest as a zero value. <langsyntaxhighlight Fortranlang="fortran"> MODULE SIMPLELINKEDLIST !Play with an array. Other arrays might hold content.
CONTAINS !Demonstration only!
SUBROUTINE LLREMOVE(LINK,X) !Remove entry X from the links in LINK.
Line 529 ⟶ 701:
CALL LLFOLLOW(LINK) !But, I know where it was, in this example.
 
END</langsyntaxhighlight>
Output:
<pre>
Line 562 ⟶ 734:
In messing with linked-lists, one must give close attention to just how an element is identified. Is element X (for removal) the X'th element in sequence along the linked list (first, second, third, etc.), or, is it the element at at a specified memory address or index position X in the LIST array (as here), or, is it the element whose cargo matches X?
 
The code involves repeated mention of <code>LINK(IT)</code> and for those who do not have total faith in the brilliance of code generated by a compiler, one could try <langsyntaxhighlight Fortranlang="fortran"> IT = 0 !This list element fingers the start of the list..
1 NEXT = LINK(IT) !This is the node of interest.
IF (NEXT.GT.0) THEN !Is it a live node?
Line 572 ⟶ 744:
GO TO 1 !And try afresh.
END IF !So much for that node.
</syntaxhighlight>
</lang>
The introduction of a mnemonic "NEXT" might help the interpretation of the code, but one must be careful about phase: NEXT is the "nextness" for IT which fingers node NEXT which is the candidate for matching against X, not IT. Alternatively, use "FROM" for IT and "IT" for NEXT, being careful to keep it straight.
 
And ... there is a blatant GO TO (aside from the equivalent concealed via RETURN) but using a WHILE-loop would require a repetition of NEXT = LINK(IT). If Fortran were to enable assignment within an expression (as in Algol) then <langsyntaxhighlight Fortranlang="fortran"> IT = 0 !This list element fingers the start of the list..
DO WHILE((NEXT = LINK(IT)).GT.0) !Finger the follower of IT.
IF (NEXT.EQ.X) THEN !Is it the unwanted one?
Line 583 ⟶ 755:
IT = NEXT !Advance to the follower.
END DO !Ends when node IT's follower is null.
</syntaxhighlight>
</lang>
No label, no nasty literal GO TO - even though an "END DO" hides one.
 
=={{header|FreeBASIC}}==
{{trans|Yabasic}}
<langsyntaxhighlight lang="freebasic">#define FIL 1
#define DATO 2
#define LINK 3
Line 655 ⟶ 827:
Print
printNode(1)
printNode(2)</langsyntaxhighlight>
{{out}}
<pre>1000
Line 666 ⟶ 838:
=={{header|Go}}==
This reuses code from other singly-linked list tasks.
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 752 ⟶ 924:
fmt.Println("\nAfter removing burritos:")
head.Traverse()
}</langsyntaxhighlight>
 
{{out}}
Line 788 ⟶ 960:
to take advantage of jq's tail-call optimization (TCO).
 
<langsyntaxhighlight lang="jq"># Input: a JSON object representing a SLL
# Output: an object with the same value after
# removal of the first item for which (.item|f) is truthy
Line 811 ⟶ 983:
else .
end;
r;</langsyntaxhighlight>
'''Example'''
<langsyntaxhighlight lang="jq">{
"item": 1,
"next": {
Line 820 ⟶ 992:
}
}
| remove_all(. == 1)</langsyntaxhighlight>
{{out}}
<pre>
Line 832 ⟶ 1,004:
{{works with|Julia|0.6}}
See the <tt>LinkedList</tt> defined at [[Singly-linked_list/Element_definition#Julia]].
<langsyntaxhighlight lang="julia">function Base.deleteat!(ll::LinkedList, index::Integer)
if isempty(ll) throw(BoundsError()) end
if index == 1
Line 848 ⟶ 1,020:
end
return ll
end</langsyntaxhighlight>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.2
 
class Node<T: Number>(var data: T, var next: Node<T>? = null) {
Line 898 ⟶ 1,070:
remove(a, b) // remove last node
println("After 2nd removal : $a")
}</langsyntaxhighlight>
 
{{out}}
Line 907 ⟶ 1,079:
After 2nd removal : 1
</pre>
 
=={{header|MiniScript}}==
We're choosing here to use the built-in list type, rather than make our own from scratch, since this is more representative of how one is likely to actually use MiniScript.
<syntaxhighlight lang="miniscript">
> myList = range(100, 110)
> myList
[100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110]
> myList.pop // removes the last element
110
> myList
[100, 101, 102, 103, 104, 105, 106, 107, 108, 109]
> myList.pull // removes the first element
100
> myList
[101, 102, 103, 104, 105, 106, 107, 108, 109]
> myList.remove 5 // removes 106 at index 5
> myList
[101, 102, 103, 104, 105, 107, 108, 109]
</syntaxhighlight>
 
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strutils
 
type
Line 981 ⟶ 1,173:
echo "After removing 1: ", list
list.remove(list.find(5))
echo "After removing 5: ", list</langsyntaxhighlight>
 
{{out}}
Line 995 ⟶ 1,187:
and then mix them up again and remove items in a random order. Obviously remove_item() forms the meat
of the task requirement; insert_inorder(), show(), and test() aren't really and can be skipped.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>enum NEXT,DATA
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
sequence sll = {}
<span style="color: #008080;">enum</span> <span style="color: #000000;">NEXT</span><span style="color: #0000FF;">,</span><span style="color: #000000;">DATA</span>
integer sll_head = 0,
<span style="color: #004080;">sequence</span> <span style="color: #000000;">sll</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
sll_free = 0
<span style="color: #004080;">integer</span> <span style="color: #000000;">sll_head</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span>
 
<span style="color: #000000;">sll_free</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
procedure insert_inorder(object data)
if length(sll)=0 or sll_head=0 then
<span style="color: #008080;">procedure</span> <span style="color: #000000;">insert_inorder</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">)</span>
sll = {{0,data}}
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sll</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">or</span> <span style="color: #000000;">sll_head</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
sll_head = 1
<span style="color: #000000;">sll</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">data</span><span style="color: #0000FF;">}}</span>
sll_free = 0
<span style="color: #000000;">sll_head</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
else
<span style="color: #000000;">sll_free</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
integer this = sll_head, next, flag = 0
<span style="color: #008080;">else</span>
object node = 0
<span style="color: #004080;">integer</span> <span style="color: #000000;">curr</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sll_head</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">next</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">flag</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
while 1 do
<span style="color: #004080;">object</span> <span style="color: #000000;">node</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
next = sll[this][NEXT]
<span style="color: #008080;">while</span> <span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
if data<sll[this][DATA] then
<span style="color: #000000;">next</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sll</span><span style="color: #0000FF;">[</span><span style="color: #000000;">curr</span><span style="color: #0000FF;">][</span><span style="color: #000000;">NEXT</span><span style="color: #0000FF;">]</span>
flag = 1
<span style="color: #008080;">if</span> <span style="color: #000000;">data</span><span style="color: #0000FF;"><</span><span style="color: #000000;">sll</span><span style="color: #0000FF;">[</span><span style="color: #000000;">curr</span><span style="color: #0000FF;">][</span><span style="color: #000000;">DATA</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
node = sll[this]
<span style="color: #000000;">flag</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
elsif next=0 then
<span style="color: #000000;">node</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sll</span><span style="color: #0000FF;">[</span><span style="color: #000000;">curr</span><span style="color: #0000FF;">]</span>
flag = 2
<span style="color: #008080;">elsif</span> <span style="color: #000000;">next</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
node = {0,data}
<span style="color: #000000;">flag</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span>
end if
<span style="color: #000000;">node</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">data</span><span style="color: #0000FF;">}</span>
if flag then
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if sll_free then
<span style="color: #008080;">if</span> <span style="color: #000000;">flag</span> <span style="color: #008080;">then</span>
next = sll_free
<span style="color: #008080;">if</span> <span style="color: #000000;">sll_free</span> <span style="color: sll[next][NEXT]#008080;">then</span>
<span style="color: #000000;">next</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sll_free</span>
sll[next] = node
<span style="color: #000000;">sll_free</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sll</span><span style="color: #0000FF;">[</span><span style="color: #000000;">next</span><span style="color: #0000FF;">][</span><span style="color: #000000;">NEXT</span><span style="color: #0000FF;">]</span>
else
<span style="color: #000000;">sll</span><span style="color: #0000FF;">[</span><span style="color: #000000;">next</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">node</span>
sll = append(sll,node)
<span next style="color: length(sll)#008080;">else</span>
<span style="color: #000000;">sll</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sll</span><span style="color: #0000FF;">,</span><span style="color: #000000;">node</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #000000;">next</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sll</span><span style="color: #0000FF;">)</span>
sll[this][NEXT] = next
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if flag=1 then
<span style="color: #000000;">node</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sll</span><span style="color: #0000FF;">[</span><span style="color: #000000;">curr</span><span style="color: #0000FF;">])</span>
sll[this][DATA] = data
<span style="color: #000000;">node</span><span style="color: #0000FF;">[</span><span style="color: #000000;">NEXT</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">next</span>
end if
<span style="color: #008080;">if</span> <span style="color: #000000;">flag</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
exit
<span style="color: #000000;">node</span><span style="color: #0000FF;">[</span><span style="color: #000000;">DATA</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">data</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
this = next
<span style="color: #000000;">sll</span><span style="color: #0000FF;">[</span><span style="color: #000000;">curr</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">node</span>
end while
<span style="color: #008080;">exit</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end procedure
<span style="color: #000000;">curr</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">next</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
procedure remove_item(object data)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
integer idx = sll_head, prev
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
while idx do
if sll[idx][DATA]=data then
<span style="color: #008080;">procedure</span> <span style="color: #000000;">remove_item</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">)</span>
if idx=sll_head then
<span style="color: #004080;">integer</span> <span style="color: #000000;">idx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sll_head</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">prev</span>
sll_head = sll[idx][NEXT]
<span style="color: #008080;">while</span> <span style="color: #000000;">idx</span> <span style="color: #008080;">do</span>
else
<span style="color: #008080;">if</span> <span style="color: #000000;">sll</span><span style="color: #0000FF;">[</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">DATA</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">data</span> <span style="color: #008080;">then</span>
sll[prev][NEXT] = sll[idx][NEXT]
<span style="color: #008080;">if</span> <span style="color: #000000;">idx</span><span style="color: #0000FF;">=</span><span style="color: #000000;">sll_head</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">sll_head</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sll</span><span style="color: #0000FF;">[</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">NEXT</span><span style="color: #0000FF;">]</span>
sll[idx][NEXT] = sll_free
sll[idx][DATA] <span style="color: 0#008080;">else</span>
<span style="color: #000000;">sll</span><span style="color: #0000FF;">[</span><span style="color: #000000;">prev</span><span style="color: #0000FF;">][</span><span style="color: #000000;">NEXT</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sll</span><span style="color: #0000FF;">[</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">NEXT</span><span style="color: #0000FF;">]</span>
sll_free = idx
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
exit
<span style="color: #000000;">sll</span><span style="color: #0000FF;">[</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">NEXT</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sll_free</span>
end if
<span style="color: #000000;">sll</span><span style="color: #0000FF;">[</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">DATA</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
prev = idx
<span style="color: #000000;">sll_free</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">idx</span>
idx = sll[idx][NEXT]
<span style="color: #008080;">exit</span>
end while
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end procedure
<span style="color: #000000;">prev</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">idx</span>
 
<span style="color: #000000;">idx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sll</span><span style="color: #0000FF;">[</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">NEXT</span><span style="color: #0000FF;">]</span>
procedure show()
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
integer idx = sll_head
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
sequence list = {}
while idx do
<span style="color: #008080;">procedure</span> <span style="color: #000000;">show</span><span style="color: #0000FF;">()</span>
list = append(list,sll[idx][DATA])
<span style="color: #004080;">integer</span> <span style="color: #000000;">idx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sll_head</span>
idx = sll[idx][NEXT]
<span style="color: #004080;">sequence</span> <span style="color: #000000;">list</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
end while
<span style="color: #008080;">while</span> <span style="color: #000000;">idx</span> <span style="color: #008080;">do</span>
?list
<span style="color: #000000;">list</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">list</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sll</span><span style="color: #0000FF;">[</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">DATA</span><span style="color: #0000FF;">])</span>
end procedure
<span style="color: #000000;">idx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sll</span><span style="color: #0000FF;">[</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">NEXT</span><span style="color: #0000FF;">]</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
enum ADD,REMOVE
<span style="color: #0000FF;">?</span><span style="color: #000000;">list</span>
procedure test(integer mode, sequence list)
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
?{{"add","remove"}[mode],list}
for i=1 to length(list) do
<span style="color: #008080;">enum</span> <span style="color: #000000;">ADD</span><span style="color: #0000FF;">,</span><span style="color: #000000;">REMOVE</span>
if mode=ADD then
<span style="color: #008080;">procedure</span> <span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">mode</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">list</span><span style="color: #0000FF;">)</span>
insert_inorder(list[i])
<span style="color: #0000FF;">?{{</span><span style="color: #008000;">"add"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"remove"</span><span style="color: #0000FF;">}[</span><span style="color: #000000;">mode</span><span style="color: #0000FF;">],</span><span style="color: #000000;">list</span><span style="color: #0000FF;">}</span>
else
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">list</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
remove_item(list[i])
<span style="color: #008080;">if</span> <span style="color: #000000;">mode</span><span style="color: #0000FF;">=</span><span style="color: #000000;">ADD</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">insert_inorder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">list</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
show()
<span style="color: #008080;">else</span>
end for
<span style="color: #000000;">remove_item</span><span style="color: #0000FF;">(</span><span style="color: #000000;">list</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
 
<span style="color: #000000;">show</span><span style="color: #0000FF;">()</span>
sequence list = {"1","2","3","4"}
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
test(ADD,shuffle(list))
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
test(REMOVE,shuffle(list))</lang>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">list</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"1"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"2"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"3"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"4"</span><span style="color: #0000FF;">}</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ADD</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">shuffle</span><span style="color: #0000FF;">(</span><span style="color: #000000;">list</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">REMOVE</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">shuffle</span><span style="color: #0000FF;">(</span><span style="color: #000000;">list</span><span style="color: #0000FF;">))</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,098 ⟶ 1,295:
=={{header|PicoLisp}}==
===Non destructive===
<langsyntaxhighlight PicoLisplang="picolisp">(de delnon (Item Lst)
(if (index Item Lst)
(conc
Line 1,108 ⟶ 1,305:
(println 'L L)
(setq L (delnon N L))
(println 'fin L) )</langsyntaxhighlight>
{{out}}
<pre>
Line 1,115 ⟶ 1,312:
</pre>
===Destructive===
<langsyntaxhighlight PicoLisplang="picolisp">(de deldestr (Item "Var")
(let Lst (val "Var")
(let? M (member Item Lst)
Line 1,125 ⟶ 1,322:
(println 'L L)
(deldestr N 'L)
(println 'fin 'L L) )</langsyntaxhighlight>
{{out}}
<pre>
Line 1,135 ⟶ 1,332:
I added a lot of comments to be more beginnner friendly. This program will remove elements in any position based on their value, not key.
 
<langsyntaxhighlight lang="python">
class Node:
def __init__(self, data=None):
Line 1,199 ⟶ 1,396:
mylist.print_llist()
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,220 ⟶ 1,417:
This is written entirely in terms of car and cdr (the linked list/pair primitives in Racket and Scheme). Usually, you'd have reverse and append available to you... but, again, it's interesting to see how they are implemented (by me, at least)
 
<langsyntaxhighlight lang="racket">#lang racket/base
 
(define (rev l (acc null))
Line 1,244 ⟶ 1,441:
(displayln (remove-at '(1 2 3) 1))
(displayln (remove-at '(1 2 3) 2))
(displayln (remove-at '(1 2 3) 3))</langsyntaxhighlight>
 
{{out}}
Line 1,257 ⟶ 1,454:
Extending <tt>class Cell</tt> from [[Singly-linked_list/Element_definition#Raku]]:
 
<syntaxhighlight lang="raku" perl6line> method delete ($value --> Cell) {
my $prev = Nil;
my $cell = self;
Line 1,276 ⟶ 1,473:
return $new-head;
}</langsyntaxhighlight>
 
Usage:
 
<syntaxhighlight lang="raku" perl6line>my $list = cons 10, (cons 20, (cons 10, (cons 30, Nil)));
 
$list = $list.delete(10);</langsyntaxhighlight>
 
=={{header|Visual Basic .NET}}==
Line 1,296 ⟶ 1,493:
- The entry has been removed.
 
<langsyntaxhighlight lang="vbnet">
 
Module Module1
Line 1,352 ⟶ 1,549:
End Module
 
</syntaxhighlight>
</lang>
 
=={{header|Wren}}==
{{libheader|Wren-llist}}
<langsyntaxhighlight ecmascriptlang="wren">import "./llist" for LinkedList
 
var ll = LinkedList.new(["dog", "cat", "bear"])
Line 1,363 ⟶ 1,560:
System.print("After removal 1: %(ll)")
ll.removeAt(0) // remove by index
System.print("After removal 2: %(ll)")</langsyntaxhighlight>
 
{{out}}
Line 1,373 ⟶ 1,570:
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Singly-linked_list/Element_insertion & removal
// by Galileo, 02/2022
 
Line 1,447 ⟶ 1,644:
print
printNode(1)
printNode(2)</langsyntaxhighlight>
{{out}}
<pre>1000
9,476

edits