Doubly-linked list/Element removal: Difference between revisions

Added FreeBASIC
(Added FreeBASIC)
(4 intermediate revisions by 3 users not shown)
Line 6:
 
You may wish to use the list element defined in [[Doubly-Linked List (element)]] for the purposes of this task.
 
=={{header|Action!}}==
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}}
<syntaxhighlight lang="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!
 
DEFINE PTR="CARD"
DEFINE NODE_SIZE="6"
TYPE ListNode=[PTR data,prv,nxt]
 
ListNode POINTER listBegin,listEnd
 
PROC Append(CHAR ARRAY v)
ListNode POINTER n
 
n=Alloc(NODE_SIZE)
n.data=v
n.prv=listEnd
n.nxt=0
IF listEnd THEN
listEnd.nxt=n
ELSE
listBegin=n
FI
listEnd=n
RETURN
 
PROC Remove(ListNode POINTER n)
ListNode POINTER prev,next
IF n=0 THEN Break() FI
 
prev=n.prv
next=n.nxt
IF prev THEN
prev.nxt=next
ELSE
listBegin=next
FI
IF next THEN
next.prv=prev
ELSE
listEnd=prev
FI
 
Free(n,NODE_SIZE)
RETURN
 
PROC PrintList()
ListNode POINTER n
 
n=listBegin
Print("(")
WHILE n
DO
Print(n.data)
IF n.nxt THEN
Print(", ")
FI
n=n.nxt
OD
PrintE(")") PutE()
RETURN
 
PROC TestRemove(ListNode POINTER n)
PrintF("Remove ""%S"":%E",n.data)
Remove(n)
PrintList()
RETURN
 
PROC Main()
Put(125) PutE() ;clear screen
AllocInit(0)
listBegin=0
listEnd=0
 
Append("First")
Append("Second")
Append("Third")
Append("Fourth")
Append("Fifth")
PrintList()
 
TestRemove(listBegin.nxt)
TestRemove(listEnd.prv)
TestRemove(listBegin)
TestRemove(listEnd)
TestRemove(listBegin)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Doubly-linked_list_element_removal.png Screenshot from Atari 8-bit computer]
<pre>
(First, Second, Third, Fourth, Fifth)
 
Remove "Second":
(First, Third, Fourth, Fifth)
 
Remove "Fourth":
(First, Third, Fifth)
 
Remove "First":
(Third, Fifth)
 
Remove "Fifth":
(Third)
 
Remove "Third":
()
</pre>
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Containers.Indefinite_Doubly_Linked_Lists;
with Ada.Text_Io;
 
Line 43 ⟶ 156:
Print_List (List);
 
end Element_Remove;</langsyntaxhighlight>
{{out}}
<pre>cat dog hen horse
Line 50 ⟶ 163:
=={{header|ALGOL W}}==
Uses the element type and insertion method as in the [[Doubly-Linked List (traversal)]] task.
<langsyntaxhighlight lang="algolw">begin
% record type to hold an element of a doubly linked list of integers %
record DListIElement ( reference(DListIElement) prev
Line 127 ⟶ 240:
end while_e_ne_null
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 149 ⟶ 262:
9000
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vbnet">Type Nodo
valor As String
sgte As Nodo Ptr
End Type
 
Sub MuestraLista(s As String, Byref cabeza As Nodo Ptr)
Dim As Nodo Ptr e = cabeza
Print s & ": { ";
While e <> 0
Print e->valor; " ";
e = e->sgte
Wend
Print "}"
End Sub
 
Sub Eliminar(Byref cabeza As Nodo Ptr, Byref objetivo As Nodo Ptr)
Dim As Nodo Ptr prev = 0
Dim As Nodo Ptr actual = cabeza
While actual <> objetivo And actual <> 0
prev = actual
actual = actual->sgte
Wend
If actual = objetivo Then
If prev = 0 Then ' cabeza node
cabeza = cabeza->sgte
Else
prev->sgte = actual->sgte
End If
Deallocate(actual)
End If
End Sub
 
Dim As Nodo Ptr dll = 0
Dim As Nodo Ptr e1 = Callocate(1, Sizeof(Nodo))
e1->valor = "dog"
e1->sgte = dll
dll = e1
 
Dim As Nodo Ptr e2 = Callocate(1, Sizeof(Nodo))
e2->valor = "cat"
e2->sgte = dll
dll = e2
 
Dim As Nodo Ptr e3 = Callocate(1, Sizeof(Nodo))
e3->valor = "bear"
e3->sgte = dll
dll = e3
 
MuestraLista("Before removals", dll)
Eliminar(dll, e2) ' remove "cat"
MuestraLista("After removal 1", dll)
Eliminar(dll, e1) ' remove "dog"
MuestraLista("After removal 2", dll)
 
Sleep</syntaxhighlight>
{{out}}
<pre>Before removals: { bear cat dog }
After removal 1: { bear dog }
After removal 2: { bear }</pre>
 
=={{header|Go}}==
Using the doubly-linked list from the container/list package and the Wren example:
<langsyntaxhighlight lang="go">package main
 
import (
Line 177 ⟶ 351:
dll.Remove(e1) // remove "dog"
printDll("After removal 2", dll)
}</langsyntaxhighlight>
 
{{out}}
Line 188 ⟶ 362:
=={{header|Julia}}==
Uses code from [[Doubly-Linked List (traversal)]]
<langsyntaxhighlight lang="julia">mutable struct DLNode{T}
value::T
pred::Union{DLNode{T}, Nothing}
Line 259 ⟶ 433:
delete(node2)
println("Then deleting node2 yields: "); printconnected(node1)
</langsyntaxhighlight>{{out}}
<pre>
From beginning to end: 1 -> 2 -> 3 -> 4
Line 270 ⟶ 444:
=={{header|Nim}}==
This is a simplified version of code from [[Doubly-Linked List (traversal)]]
<langsyntaxhighlight Nimlang="nim">type
 
DoublyLinkedList[T] = object
Line 326 ⟶ 500:
echo l
l.remove c
echo l</langsyntaxhighlight>
 
{{out}}
Line 338 ⟶ 512:
=={{header|Phix}}==
Extended copy of [[Doubly-linked_list/Traversal#Phix]]
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">enum</span> <span style="color: #000000;">NEXT</span><span style="color: #0000FF;">,</span><span style="color: #000000;">PREV</span><span style="color: #0000FF;">,</span><span style="color: #000000;">DATA</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">empty_dll</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}}</span>
Line 389 ⟶ 563:
<span style="color: #000000;">show</span><span style="color: #0000FF;">(</span><span style="color: #000000;">PREV</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 419 ⟶ 593:
=={{header|Wren}}==
{{libheader|Wren-llist}}
<langsyntaxhighlight ecmascriptlang="wren">import "./llist" for DLinkedList
 
var dll = DLinkedList.new(["dog", "cat", "bear"])
Line 426 ⟶ 600:
System.print("After removal 1: %(dll)")
dll.removeAt(0) // remove by index
System.print("After removal 2: %(dll)")</langsyntaxhighlight>
 
{{out}}
2,136

edits