Skip to content

Instantly share code, notes, and snippets.

@aykutyaman
Created August 31, 2018 20:16
Show Gist options
  • Save aykutyaman/16c38d7e07f52a0867f7a99f1ddf9298 to your computer and use it in GitHub Desktop.
Save aykutyaman/16c38d7e07f52a0867f7a99f1ddf9298 to your computer and use it in GitHub Desktop.
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