Skip to content

Instantly share code, notes, and snippets.

@bhelyer
Created July 4, 2018 10:24
Show Gist options
  • Select an option

  • Save bhelyer/2e74ecc9970025ab5fbbfa68a984dd99 to your computer and use it in GitHub Desktop.

Select an option

Save bhelyer/2e74ecc9970025ab5fbbfa68a984dd99 to your computer and use it in GitHub Desktop.
module main;
import io = watt.io;
class LinkedList!(T)
{
private:
mData: T;
mNext: LinkedList;
public:
this()
{
}
this(val: T)
{
mData = val;
}
public:
fn length() size_t
{
current := mNext;
count: size_t;
while (current !is null) {
count++;
current = current.mNext;
}
return count;
}
fn add(val: T)
{
if (mNext is null) {
mNext = new LinkedList(val);
return;
}
current := mNext;
while (current !is null) {
if (current.mNext is null) {
current.mNext = new LinkedList(val);
return;
}
current = current.mNext;
}
}
fn iterate(cb: scope dg(T))
{
current := mNext;
while (current !is null) {
cb(current.mData);
current = current.mNext;
}
}
}
class IntegerList = LinkedList!i32;
fn main() i32
{
checkVal := 1;
checkCounter := 0;
fn printInteger(val: i32) { io.writeln(val); if (checkVal++ == val) { checkCounter++; }}
il := new IntegerList();
il.add(1);
il.add(2);
il.add(3);
if (il.length() != 3) {
return 1;
}
il.iterate(printInteger);
return checkCounter - 3;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment