All examples
Language

A basic integer linked list

Build Node and LinkedList classes, append or prepend integers, and traverse nullable links after checking for null.

Download .aner source
linkedlist_basic.aner
// Basic singly linked list of Int64 values.
class Node {
    public let value: Int64
    public var next: Node?
}

class LinkedList {
    var head: Node?

    fn prepend(value: Int64) {
        head = Node(value: value, next: head)
    }

    fn append(value: Int64) {
        let node = Node(value: value, next: null)
        if head == null {
            head = node
            return
        }
        var cursor = head
        while cursor.next != null {
            cursor = cursor.next
        }
        cursor.next = node
    }

    fn len() {
        var count = 0
        var cursor = head
        while cursor != null {
            count = count + 1
            cursor = cursor.next
        }
        return count
    }
}

let numbers = LinkedList(head: null)
numbers.append(20)
numbers.prepend(10)
numbers.append(30)
print(numbers.len()) // 3

Make it your experiment.

With Aner installed, save this program as examples/linkedlist_basic.aner inside a folder for your experiment. Open a terminal in that folder, then check and run the program.

Terminal
aner check examples/linkedlist_basic.aner
aner run examples/linkedlist_basic.aner

The aner command must be on your PATH. Follow the installation guide if your terminal cannot find it.

Iris and Wine are teaching datasets with their own attribution. Example outcomes are not comparative benchmarks or evidence of clinical validity.

Dataset sources & attribution
Install Aner