Singly-Linked List (element)
singly-linked list
Using this method, insert an element C into a list comprised of elements A->B, following element A.

See also

 Array
 Associative array: Creation, Iteration
 Collections
 Compound data type
 Doubly-linked list: Definition, Element definition, Element insertion, List Traversal, Element Removal
 Linked list
 Queue: Definition, Usage
 Set
 Singly-linked list: Element definition, Element insertion, List Traversal, Element Removal
 Stack


MODULE SinglyLinkedList EXPORTS Main;
 
TYPE
  Link = REF LinkRcd;
  LinkRcd = RECORD
    Next: Link;
    Data: INTEGER
  END;
 
  PROCEDURE InsertAppend(anchor, next: Link) =
  BEGIN
    IF anchor # NIL AND next # NIL THEN
      next.Next := anchor.Next;
      anchor.Next := next
    END
  END InsertAppend;
 
VAR
  a: Link := NEW(Link, Next := NIL, Data := 1);
  b: Link := NEW(Link, Next := NIL, Data := 2);
  c: Link := NEW(Link, Next := NIL, Data := 3);
 
BEGIN
  InsertAppend(a, b);
  InsertAppend(a, c)
END SinglyLinkedList.