Skip to content

Instantly share code, notes, and snippets.

@hami9x
Last active November 11, 2018 19:17
Show Gist options
  • Save hami9x/06c78f83f7260c2ddc85a7bac3562745 to your computer and use it in GitHub Desktop.
Save hami9x/06c78f83f7260c2ddc85a7bac3562745 to your computer and use it in GitHub Desktop.
LinkedList in Chi
struct Node<any T> {
T data;
Node<T>* next = 0;
void new(T data) {
this.data = data;
}
};
struct LinkedList<any T> {
Node<T>* head = 0;
Node<T>* tail = 0;
int size = 0;
Node<T>* add(T value) {
this.tail = new {value};
if (!this.head) {
this.head = this.tail;
} else {
let node = this.head;
for (;;) {
if (!node.next) {
break;
}
node = node.next;
}
node.next = this.tail;
}
++this.size;
return this.tail;
}
}
int main() {
LinkedList<int> list;
list.add(1);
printf("list.size: {}\n", list.size);
printf("list.tail: {}\n", list.tail.data);
list.add(2);
printf("list.size: {}\n", list.size);
printf("list.tail: {}\n", list.tail.data);
for (let node=list.head; node; node = node.next) {
printf("item value: {}\n", node.data);
}
return 0;
}
list.size: 1
list.tail: 1
list.size: 2
list.tail: 2
item value: 1
item value: 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment