Doubly-linked list/Element insertion: Difference between revisions

m
(Added solution for Action!)
m (→‎{{header|Wren}}: Minor tidy)
 
(5 intermediate revisions by 5 users not shown)
Line 8:
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!
 
DEFINE PTR="CARD"
DEFINE NODE_SIZE="5"
TYPE ListNode=[CHAR data CARDPTR prv,nxt]
 
ListNode POINTER listBegin,listEnd
Line 125 ⟶ 126:
TestAddAfter('C,listBegin)
TestClear()
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Doubly-linked_list_element_insertion.png Screenshot from Atari 8-bit computer]
Line 143 ⟶ 144:
 
Define the procedure:
<langsyntaxhighlight lang="ada">procedure Insert (Anchor : Link_Access; New_Link : Link_Access) is
begin
if Anchor /= Null and New_Link /= Null then
Line 153 ⟶ 154:
Anchor.Next := New_Link;
end if;
end Insert;</langsyntaxhighlight>
Create links and form the list.
 
<langsyntaxhighlight lang="ada">procedure Make_List is
A, B, C : Link_Access;
begin
Line 167 ⟶ 168:
Insert(Anchor => A, New_Link => B); -- The list is (A, B)
Insert(Anchor => A, New_Link => C); -- The list is (A, C, B)
end Make_List;</langsyntaxhighlight>
 
Element insertion using the generic doubly linked list defined in the standard Ada containers library.
 
<langsyntaxhighlight lang="ada">with Ada.Containers.Doubly_Linked_Lists;
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
Line 189 ⟶ 190:
New_Item => To_Unbounded_String("C"));
The_List.Iterate(Print'access);
end List_Insertion;</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
Line 195 ⟶ 196:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny].}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
<langsyntaxhighlight lang="algol68">#!/usr/local/bin/a68g --script #
 
# SEMA do link OF splice = LEVEL 1 #
Line 245 ⟶ 246:
OD;
print(new line)
)</langsyntaxhighlight>
Output:
<pre>
Line 252 ⟶ 253:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw"> % record type to hold an element of a doubly linked list of integers %
record DListIElement ( reference(DListIElement) prev
; integer iValue
Line 274 ⟶ 275:
end if_e_ne_null ;
newElement
end insertIntoDListiAfter ;</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
Line 280 ⟶ 281:
 
=={{header|Axe}}==
<langsyntaxhighlight lang="axe">Lbl INSERT
{r₁+2}ʳ→{r₂+2}ʳ
r₁→{r₂+4}ʳ
Line 286 ⟶ 287:
r₂→{r₁+2}ʳ
r₁
Return</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> DIM node{pPrev%, pNext%, iData%}
DIM a{} = node{}, b{} = node{}, c{} = node{}
Line 310 ⟶ 311:
here.pNext% = new{}
ENDPROC
</syntaxhighlight>
</lang>
 
=={{header|C}}==
Define the function:
<langsyntaxhighlight lang="c">void insert(link* anchor, link* newlink) {
newlink->next = anchor->next;
newlink->prev = anchor;
(newlink->next)->prev = newlink;
anchor->next = newlink;
}</langsyntaxhighlight>
 
Production code should also include checks that the passed links are valid (e.g. not null pointers). There should also be code to handle special cases, such as when *anchor is the end of the existing list (i.e. anchor->next is a null pointer).
Line 327 ⟶ 328:
 
Create links, and form the list:
<langsyntaxhighlight lang="c">link a, b, c;
a.next = &b;
a.prev = null;
Line 334 ⟶ 335:
b.prev = &a;
b.data = 3;
c.data = 2;</langsyntaxhighlight>
 
This list is now {a,b}, and c is separate.
 
Now call the function:
<syntaxhighlight lang ="c">insert(&a, &c);</langsyntaxhighlight>
 
This function call changes the list from {a,b} to {a,b,c}.
Line 345 ⟶ 346:
=={{header|C sharp|C#}}==
The function handles creation of nodes in addition to inserting them.
<langsyntaxhighlight lang="csharp">static void InsertAfter(Link prev, int i)
{
if (prev.next != null)
Line 354 ⟶ 355:
else
prev.next = new Link() { item = i, prev = prev };
}</langsyntaxhighlight>
Example use:
<langsyntaxhighlight lang="csharp">static void Main()
{
//Create A(5)->B(7)
Line 363 ⟶ 364:
//Insert C(15) between A and B
InsertAfter(A, 15);
}</langsyntaxhighlight>
 
=={{header|C++}}==
C++ already has own linked list structure. If we were to roll our own linked list, we could implement function inserting new value after specific node like that:
<langsyntaxhighlight lang="cpp">template <typename T>
void insert_after(Node<T>* N, T&& data)
{
Line 374 ⟶ 375:
N->next->prev = node;
N->next = node;
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
Line 380 ⟶ 381:
This sort of mutable structure is not idiomatic in Clojure. [[../Definition#Clojure]] or a finger tree implementation would be better.
 
<langsyntaxhighlight Clojurelang="clojure">(defrecord Node [prev next data])
 
(defn new-node [prev next data]
Line 403 ⟶ 404:
t (new-node nil nil :B)]
(insert-between h t (new-node nil nil :C))
(new-list h t))</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
Line 409 ⟶ 410:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio;
 
struct Node(T) {
Line 443 ⟶ 444:
insertAfter(list, "C");
list.show;
}</langsyntaxhighlight>
{{out}}
<pre>A
Line 451 ⟶ 452:
{{libheader| System.SysUtils}}
{{libheader|Boost.LinkedList}}[https://github.com/MaiconSoft/DelphiBoostLib]
<syntaxhighlight lang="delphi">
<lang Delphi>
program Element_insertion;
 
Line 470 ⟶ 471:
List.Free;
Readln;
end.</langsyntaxhighlight>
{{out}}
<pre>[ 5, 15, 7]</pre>
Line 476 ⟶ 477:
 
=={{header|E}}==
<langsyntaxhighlight lang="e">def insert(after, value) {
def newNode := makeElement(value, after, after.getNext())
after.getNext().setPrev(newNode)
after.setNext(newNode)
}</langsyntaxhighlight>
 
<syntaxhighlight lang ="e">insert(A_node, C)</langsyntaxhighlight>
 
 
Line 500 ⟶ 501:
=={{header|Fortran}}==
In ISO Fortran 95 or later:
<langsyntaxhighlight lang="fortran">module dlList
public :: node, insertAfter, getNext
Line 554 ⟶ 555:
current => next
end do
end program dlListTest</langsyntaxhighlight>
Output:
<langsyntaxhighlight lang="fortran">1.
2.
4.
Line 576 ⟶ 577:
262144.
524288.
1048576.</langsyntaxhighlight>
 
 
=={{header|FreeBASIC}}==
{{trans|Yabasic}}
<syntaxhighlight lang="freebasic">#define FIL 1
#define DATO 2
#define LPREV 3
#define LNEXT 4
 
Dim Shared As Integer countNodes, Nodes
countNodes = 0
Nodes = 10
 
Dim Shared As Integer list(Nodes, 4)
list(0, LNEXT) = 1
 
Function searchNode(node As Integer) As Integer
Dim As Integer Node11
For i As Integer = 0 To node-1
Node11 = list(Node11, LNEXT)
Next i
Return Node11
End Function
 
Sub insertNode(node As Integer, newNode As Integer)
Dim As Integer Node11, i
If countNodes = 0 Then node = 2
For i = 1 To Nodes
If Not list(i, FIL) Then Exit For
Next
list(i, FIL) = True
list(i, DATO) = newNode
Node11 = searchNode(node)
list(i, LPREV) = list(Node11, LPREV)
list(list(i, LPREV), LNEXT) = i
If i <> Node11 Then list(i, LNEXT) = Node11 : list(Node11, LPREV) = i
countNodes += 1
If countNodes = Nodes Then Nodes += 10 : Redim list(Nodes, 4)
End Sub
 
Sub removeNode(n As Integer)
Dim As Integer Node11 = searchNode(n)
list(list(Node11, LPREV), LNEXT) = list(Node11, LNEXT)
list(list(Node11, LNEXT), LPREV) = list(Node11, LPREV)
list(Node11, LNEXT) = 0
list(Node11, LPREV) = 0
list(Node11, FIL) = False
countNodes -= 1
End Sub
 
Sub printNode(node As Integer)
Dim As Integer Node11 = searchNode(node)
Print list(Node11, DATO);
Print
End Sub
 
Sub traverseList()
For i As Integer = 1 To countNodes
printNode(i)
Next i
End Sub
 
insertNode(1, 1000)
insertNode(2, 2000)
insertNode(2, 3000)
 
traverseList()
 
removeNode(2)
 
Print
traverseList()
Sleep</syntaxhighlight>
{{out}}
<pre>
Igual que la entrada de Yabasic.
</pre>
 
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 633 ⟶ 718:
dll.insertAfter(a, &dlNode{string: "C"})
fmt.Println(dll)
}</langsyntaxhighlight>
Output:
<pre>
Line 644 ⟶ 729:
Using the algebraic data type and update functions from [[Doubly-Linked_List_(element)#Haskell]].
 
<langsyntaxhighlight lang="haskell">
insert _ Leaf = Leaf
insert nv l@(Node pl v r) = (\(Node c _ _) -> c) new
Line 652 ⟶ 737:
Leaf -> Leaf
Node _ v r -> Node new v r
</syntaxhighlight>
</lang>
 
==Icon and {{header|Unicon}}==
Line 658 ⟶ 743:
Uses Unicon classes.
 
<syntaxhighlight lang="unicon">
<lang Unicon>
class DoubleLink (value, prev_link, next_link)
 
Line 674 ⟶ 759:
self.next_link := next_link
end
</syntaxhighlight>
</lang>
 
=={{header|J}}==
Line 690 ⟶ 775:
The Java implementation does not a method to add an element based on another element. However, we can use methods from the base class to create a <code>AddAfter</code> method. A class with this method inherting all methods from the <code>LinkedList<T></code> class is shown below.
 
<langsyntaxhighlight lang="java">
import java.util.LinkedList;
 
Line 731 ⟶ 816:
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 753 ⟶ 838:
=={{header|JavaScript}}==
See [[Doubly-Linked_List_(element)#JavaScript]]
<langsyntaxhighlight lang="javascript">DoublyLinkedList.prototype.insertAfter = function(searchValue, nodeToInsert) {
if (this._value == searchValue) {
var after = this.next();
Line 768 ⟶ 853:
 
var list = createDoublyLinkedListFromArray(['A','B']);
list.insertAfter('A', new DoublyLinkedList('C', null, null));</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">mutable struct DLNode{T}
value::T
pred::Union{DLNode{T}, Nothing}
Line 806 ⟶ 891:
insertpost(node1, node3)
printfromroot(node1)
</langsyntaxhighlight> {{output}} <pre>
1
1 -> 2
Line 813 ⟶ 898:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.2
 
class Node<T: Number>(var data: T, var prev: Node<T>? = null, var next: Node<T>? = null) {
Line 842 ⟶ 927:
insert(after = a, new = c)
println("After insertion : $a")
}</langsyntaxhighlight>
 
{{out}}
Line 854 ⟶ 939:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ds = CreateDataStructure["DoublyLinkedList"];
ds["Append", "A"];
ds["Append", "B"];
ds["Append", "C"];
ds["SwapPart", 2, 3];
ds["Elements"]</langsyntaxhighlight>
{{out}}
<pre>{"A", "C", "B"}</pre>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">proc insertAfter[T](l: var List[T], r, n: Node[T]) =
n.prev = r
n.next = r.next
n.next.prev = n
r.next = n
if r == l.tail: l.tail = n</langsyntaxhighlight>
 
=={{header|Oberon-2}}==
<langsyntaxhighlight lang="oberon2">
PROCEDURE (dll: DLList) InsertAfter*(p: Node; o: Box.Object);
VAR
Line 885 ⟶ 970:
INC(dll.size)
END InsertAfter;
</syntaxhighlight>
</lang>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">method : public : native : AddBack(value : Base) ~ Nil {
node := ListNode->New(value);
if(@head = Nil) {
Line 899 ⟶ 984:
@tail := node;
};
}</langsyntaxhighlight>
 
=={{header|OCaml}}==
===with dlink===
<langsyntaxhighlight lang="ocaml">(* val _insert : 'a dlink -> 'a dlink -> unit *)
let _insert anchor newlink =
newlink.next <- anchor.next;
Line 918 ⟶ 1,003:
match dl with
| (Some anchor) -> _insert anchor {data=v; prev=None; next=None}
| None -> invalid_arg "dlink empty";;</langsyntaxhighlight>
 
testing in the top level:
 
<langsyntaxhighlight lang="ocaml"># type t = A | B | C ;;
type t = A | B | C
 
Line 928 ⟶ 1,013:
insert dl C;
list_of_dlink dl ;;
- : t list = [A; C; B]</langsyntaxhighlight>
 
===with nav_list===
 
<langsyntaxhighlight lang="ocaml">(* val insert : 'a nav_list -> 'a -> 'a nav_list *)
let insert (prev, cur, next) v = (prev, cur, v::next)</langsyntaxhighlight>
 
testing in the top level:
<langsyntaxhighlight lang="ocaml"># type t = A | B | C ;;
type t = A | B | C
 
Line 943 ⟶ 1,028:
 
# insert nl C ;;
- : 'a list * t * t list = ([], A, [C; B])</langsyntaxhighlight>
 
=={{header|Oforth}}==
Line 949 ⟶ 1,034:
Complete definition is here : [[../Definition#Oforth]]
 
<langsyntaxhighlight lang="oforth">: test // ( -- aDList )
| dl |
DList new ->dl
Line 955 ⟶ 1,040:
dl insertBack("B")
dl head insertAfter(DNode new("C", null , null))
dl ;</langsyntaxhighlight>
 
{{out}}
Line 965 ⟶ 1,050:
=={{header|Oz}}==
Warning: Do not use in real programs.
<langsyntaxhighlight lang="oz">declare
fun {CreateNewNode Value}
node(prev:{NewCell nil}
Line 989 ⟶ 1,074:
in
{InsertAfter A B}
{InsertAfter A C}</langsyntaxhighlight>
 
=={{header|Pascal}}==
 
<langsyntaxhighlight lang="pascal">procedure insert_link( a, b, c: link_ptr );
begin
a^.next := c;
Line 999 ⟶ 1,084:
c^.next := b;
c^.prev := a;
end;</langsyntaxhighlight>
 
Actually, a more likely real world scenario is 'insert after A'. So...
 
<langsyntaxhighlight lang="pascal">procedure realistic_insert_link( a, c: link_ptr );
begin
if a^.next <> nil then a^.next^.prev := c; (* 'a^.next^' is another way of saying 'b', if b exists *)
Line 1,009 ⟶ 1,094:
a^.next := c;
c^.prev := a;
end;</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">my %node_model = (
data => 'something',
prev => undef,
Line 1,036 ⟶ 1,121:
# insert element C into a list {A,B}, between elements A and B.
my $node_c = { %node_model };
insert($node_a, $node_c);</langsyntaxhighlight>
 
=={{header|Phix}}==
See also [[Doubly-linked_list/Traversal#Phix|Doubly-linked_list/Traversal]].
<!--<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 1,059 ⟶ 1,144:
<span style="color: #0000FF;">?</span><span style="color: #000000;">dll</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,069 ⟶ 1,154:
[[Doubly-linked list/Definition#PicoLisp]] and
[[Doubly-linked list/Element definition#PicoLisp]].
<langsyntaxhighlight PicoLisplang="picolisp"># Insert an element X at position Pos
(de 2insert (X Pos DLst)
(let (Lst (nth (car DLst) (dec (* 2 Pos))) New (cons X (cadr Lst) Lst))
Line 1,080 ⟶ 1,165:
 
(setq *DL (2list 'A 'B)) # Build a two-element doubly-linked list
(2insert 'C 2 *DL) # Insert C at position 2</langsyntaxhighlight>
For output of the example data, see [[Doubly-linked list/Traversal#PicoLisp]].
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
define structure
1 Node,
Line 1,104 ⟶ 1,189:
Q => back_pointer = P;
/* Points the next node to the new one. */
</syntaxhighlight>
</lang>
 
=={{header|Pop11}}==
<langsyntaxhighlight lang="pop11">define insert_double(list, element);
lvars tmp;
if list == [] then
Line 1,129 ⟶ 1,214:
insert_double(A, B) -> _;
;;; insert C between A and b
insert_double(A, C) -> _;</langsyntaxhighlight>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Structure node
*prev.node
*next.node
Line 1,172 ⟶ 1,257:
Input()
CloseConsole()
EndIf</langsyntaxhighlight>
Sample output:
<pre>A C B</pre>
Line 1,178 ⟶ 1,263:
=={{header|Python}}==
 
<langsyntaxhighlight lang="python">def insert(anchor, new):
new.next = anchor.next
new.prev = anchor
anchor.next.prev = new
anchor.next = new</langsyntaxhighlight>
 
=={{header|Racket}}==
Line 1,191 ⟶ 1,276:
 
Using the generic definitions from [[Doubly-linked_list/Definition#Raku]]:
<syntaxhighlight lang="raku" perl6line>role DLElem[::T] {
has DLElem[T] $.prev is rw;
has DLElem[T] $.next is rw;
Line 1,244 ⟶ 1,329:
my $b = $sdll.first.post-insert('A').post-insert('B');
$b.pre-insert('C');
say $sdll.list; # A C B</langsyntaxhighlight>
{{out}}
<pre>A C B</pre>
Line 1,251 ⟶ 1,336:
REXX doesn't have linked lists, as there are no pointers (or handles).
<br>However, linked lists can be simulated with lists in REXX.
<langsyntaxhighlight lang="rexx">/*REXX program that implements various List Manager functions. */
/*┌────────────────────────────────────────────────────────────────────┐
┌─┘ Functions of the List Manager └─┐
Line 1,329 ⟶ 1,414:
$.@=_
call @adjust
return</langsyntaxhighlight>
'''output'''
<pre style="height:50ex;overflow:scroll">
Line 1,368 ⟶ 1,453:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">class DListNode
def insert_after(search_value, new_value)
if search_value == value
Line 1,386 ⟶ 1,471:
 
head = DListNode.from_array([:a, :b])
head.insert_after(:a, :c)</langsyntaxhighlight>
 
=={{header|Rust}}==
=== Simply using the standard library ===
<langsyntaxhighlight lang="rust">use std::collections::LinkedList;
fn main() {
let mut list = LinkedList::new();
list.push_front(8);
}</langsyntaxhighlight>
 
=== The behind-the-scenes implementation ===
This expands upon the implementation defined in [[Doubly-linked list/Element definition#The_behind-the-scenes_implementation]] and consists of the relevant lines from the LinkedList implementation in the Rust standard library.
 
<langsyntaxhighlight lang="rust">impl<T> Node<T> {
fn new(v: T) -> Node<T> {
Node {value: v, next: None, prev: Rawlink::none()}
Line 1,450 ⟶ 1,535:
self.push_front_node(Box::new(Node::new(elt)));
}
}</langsyntaxhighlight>
 
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">typealias NodePtr<T> = UnsafeMutablePointer<Node<T>>
 
class Node<T> {
Line 1,501 ⟶ 1,586:
 
// [1, 3, 2]
insert(3, after: list.pointee, list: list)</langsyntaxhighlight>
 
=={{header|Tcl}}==
Line 1,507 ⟶ 1,592:
<br>
{{works with|Tcl|8.6}}
<langsyntaxhighlight lang="tcl">oo::define List {
method insertBefore {elem} {
$elem next [self]
Line 1,524 ⟶ 1,609:
set next $elem
}
}</langsyntaxhighlight>
Demonstration:
<langsyntaxhighlight lang="tcl">set B [List new 3]
set A [List new 1 $B]
set C [List new 2]
$A insertAfter $C
puts [format "{%d,%d,%d}" [$A value] [[$A next] value] [[[$A next] next] value]]</langsyntaxhighlight>
 
=={{header|Visual Basic .NET}}==
 
<langsyntaxhighlight lang="vbnet">Public Sub Insert(ByVal a As Node(Of T), ByVal b As Node(Of T), ByVal c As T)
Dim node As New Node(Of T)(value)
a.Next = node
Line 1,540 ⟶ 1,625:
b.Previous = node
node.Next = b
End Sub</langsyntaxhighlight>
 
=={{header|Wren}}==
{{libheader|Wren-llist}}
<langsyntaxhighlight ecmascriptlang="wren">import "./llist" for DLinkedList
 
var dll = DLinkedList.new(["A", "B"])
dll.insertAfter("A", "C")
System.print(dll)</langsyntaxhighlight>
 
{{out}}
Line 1,554 ⟶ 1,639:
[A <-> C <-> B]
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">def \Node\ Prev, Data, Next; \Element (Node) definition
 
proc Insert(NewNode, Node); \Insert NewNode after Node
int NewNode, Node, NextNode;
[NextNode:= Node(Next);
NextNode(Prev):= NewNode;
NewNode(Next):= NextNode;
NewNode(Prev):= Node;
Node(Next):= NewNode;
];
 
int Node, A(3), B(3), C(3);
[A(Next):= B;
A(Data):= ^a;
B(Prev):= A;
B(Data):= ^b;
B(Next):= 0;
C(Data):= ^c;
Insert(C, A);
Node:= A;
while Node # 0 do
[ChOut(0, Node(Data));
Node:= Node(Next);
];
]</syntaxhighlight>
{{out}}
<pre>
acb
</pre>
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Doubly-linked_list/Element_insertion & removal & traverse
// by Galileo, 02/2022
 
FIL = 1 : DATO = 2 : LPREV = 3 : LNEXT = 4
countNodes = 0 : Nodes = 10
 
dim list(Nodes, 4)
 
list(0, LNEXT) = 1
 
 
sub searchNode(node)
local i, Node
for i = 0 to node-1
Node = list(Node, LNEXT)
next
return Node
end sub
 
sub insertNode(node, newNode)
local Node, i
if not countNodes node = 2
for i = 1 to Nodes
if not list(i, FIL) break
next
list(i, FIL) = true
list(i, DATO) = newNode
Node = searchNode(node)
list(i, LPREV) = list(Node, LPREV) : list(list(i, LPREV), LNEXT) = i
if i <> Node list(i, LNEXT) = Node : list(Node, LPREV) = i
countNodes = countNodes + 1
if countNodes = Nodes then Nodes = Nodes + 10 : redim list(Nodes, 4) : end if
end sub
 
 
sub removeNode(n)
local Node
Node = searchNode(n)
list(list(Node, LPREV), LNEXT) = list(Node, LNEXT)
list(list(Node, LNEXT), LPREV) = list(Node, LPREV)
list(Node, LNEXT) = 0 : list(Node, LPREV) = 0
list(Node, FIL) = false
countNodes = countNodes - 1
end sub
 
 
sub printNode(node)
local Node
Node = searchNode(node)
print list(Node, DATO);
print
end sub
 
 
sub traverseList()
local i
for i = 1 to countNodes
printNode(i)
next
end sub
 
 
insertNode(1, 1000)
insertNode(2, 2000)
insertNode(2, 3000)
 
traverseList()
 
removeNode(2)
 
print
traverseList()</syntaxhighlight>
{{out}}
<pre>1000
3000
2000
 
1000
2000
---Program done, press RETURN---</pre>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">class Node{
fcn init(_value,_prev=Void,_next=Void)
{ var value=_value, prev=_prev, next=_next; }
Line 1,566 ⟶ 1,773:
b
}
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">a:=Node("a");
a.append("b").append("c");
println(a," ",a.next," ",a.next.next);</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits