Last active
August 29, 2015 14:16
-
-
Save mattbasta/3b44b001d27bbd6b3a28 to your computer and use it in GitHub Desktop.
Super basic implementation of linked lists in BType
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object LinkedList { | |
with T; | |
LinkedListNode<T>:first; | |
LinkedListNode<T>:last; | |
int:length; | |
append(T:value) { | |
var node = new LinkedListNode<T>(self, value); | |
self.length = self.length + 1; | |
if self.first == null { | |
self.first = node; | |
self.last = node; | |
node.next = node; | |
node.previous = node; | |
return; | |
} | |
self.last.next = node; | |
node.previous = self.last; | |
self.last = node; | |
node.next = self.first; | |
} | |
T:shift() { | |
var val = self.first.value; | |
self.first = self.first.next; | |
self.last.next = self.first; | |
return val; | |
} | |
} | |
object LinkedListNode { | |
with T; | |
LinkedList<T>:parent; | |
LinkedListNode<T>:next; | |
LinkedListNode<T>:previous; | |
T:value; | |
new(LinkedList<T>:parent, T:value) { | |
self.parent = parent; | |
self.value = value; | |
} | |
} | |
func int:main() { | |
var ll = new LinkedList<int>(); | |
ll.append(1); | |
ll.append(2); | |
ll.append(3); | |
var acc = ll.shift(); | |
acc = acc + ll.shift(); | |
acc = acc + ll.shift(); | |
return acc; | |
} | |
export main; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment