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


def \Node\ Link, Data;          \linked list element definition
def IntSize = 4;                \number of bytes in an integer
 
proc Insert(List, Node);        \Insert Node into List
int  List, Node;
[Node(Link):= List(Link);
 List(Link):= Node;
];
 
int MyNode, MyList;
int A, B, C;
[A:= Reserve(2*IntSize);
 B:= Reserve(2*IntSize);
 C:= Reserve(2*IntSize);
A(Data):= 1;
B(Data):= 2;
C(Data):= 3;
MyList:= A;                     \make initial list
A(Link):= 0;
Insert(A, B);                   \insert node B after A
Insert(A, C);                   \insert node C after A
MyNode:= MyList;                \traverse the linked list
while MyNode # 0 do             \display the example data
    [IntOut(0, MyNode(Data));
    ChOut(0, ^ );
    MyNode:= MyNode(Link);      \move to next node
    ];
]