Created
August 31, 2018 20:16
-
-
Save aykutyaman/16c38d7e07f52a0867f7a99f1ddf9298 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const LinkedList = () => { | |
let length = 0; | |
let headNode = null; | |
let Node = (element) => ({ | |
element, | |
next: null, | |
}); | |
let size = () => length; | |
let head = () => headNode; | |
let add = (element) => { | |
let node = Node(element); | |
if (headNode === null) { | |
headNode = node; | |
} else { | |
let currentNode = headNode; | |
while (currentNode.next) { | |
currentNode = currentNode.next; | |
} | |
currentNode.next = node; | |
} | |
length++; | |
}; | |
return { | |
size, | |
head, | |
add, | |
}; | |
}; | |
let cities = LinkedList(); | |
cities.add('ankara'); | |
cities.add('milano'); | |
cities.add('berlin'); | |
console.log(cities.size() === 3); | |
let animals = LinkedList(); | |
animals.add('lion'); | |
animals.add('elephant'); | |
console.log(animals.head().element === 'lion'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment