Last active
March 2, 2018 19:10
-
-
Save lucasdinonolte/3981959 to your computer and use it in GitHub Desktop.
Data Structures in CoffeeScript – a growing collection
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
class Node | |
cargo : null | |
left : null | |
right : null | |
constructor : (@cargo) -> | |
class Tree | |
root : null | |
current : null | |
constructor : (@root) -> | |
insertNode : (cargo) -> | |
node = @root | |
while node | |
if cargo >= node.cargo | |
if node.right isnt null | |
node = node.right | |
else | |
node.right = new Node cargo | |
break | |
else | |
if node.left isnt null | |
node = node.left | |
else | |
node.left = new Node cargo | |
break | |
inorder = (node) -> | |
return if node is null | |
# Recursive | |
inorder node.left | |
console.log node.cargo | |
inorder node.right | |
# Build a tree | |
tree = new Tree | |
tree.root = new Node 20 | |
for i in [1..50] | |
rand = Math.round(Math.random() * 100) | |
tree.insertNode rand | |
# Traverse that tree | |
inorder tree.root |
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
# Listen Container | |
class List | |
last: null | |
first: null | |
add: (cargo) -> | |
el = new Node(cargo) | |
el.prev = @last | |
if(@last isnt null) | |
@last.next = el | |
if(@first isnt null) | |
@first = el | |
@last = el | |
# Listenelemete | |
class Node | |
next: null | |
prev: null | |
constructor: (@cargo) -> | |
# Liste anlegen und füllen | |
list = new List | |
for i in [0..10] | |
list.add i |
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
# Queue Data Structure in CoffeeScript | |
# uses the First-In First-Out Principle | |
# Element class | |
class Element | |
constructor: (@cargo, @next) -> | |
# Queue Class | |
class Queue | |
constructor : () -> | |
@head = null | |
@pointer = null | |
add : (cargo) -> | |
el = new Element cargo, @pointer | |
if @pointer isnt null | |
@pointer.next = el | |
@pointer = el | |
if @head is null | |
@head = el | |
get : () -> | |
val = @head | |
@head = @head.next | |
return val | |
# Test the Queue | |
# Create a Queue | |
myQueue = new Queue() | |
# Fill it | |
for i in [0..10] | |
myQueue.add i | |
# Output it | |
for i in [0..10] | |
console.log myQueue.get().cargo |
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
# A quite nice stack in coffeescript | |
# uses the Last-In First-Out Principle | |
# Element Class | |
class Element | |
constructor: (@cargo, @next) -> | |
# Stack Class | |
class Stack | |
constructor : () -> | |
@head = null | |
push : (cargo) -> | |
el = new Element cargo, @head | |
@head = el | |
pop : () -> | |
val = @head | |
@head = @head.next | |
return val | |
# Test the Stack | |
# Create a stack | |
myStack = new Stack() | |
# Fill the stack | |
for i in [0..10] | |
myStack.push i | |
# Output the stack | |
for i in [0..10] | |
console.log myStack.pop().cargo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nice work, your Queue totally saved my day!