Last active
January 6, 2019 09:48
-
-
Save kenigbolo/6114d184afae69173561c86ed2d028fc to your computer and use it in GitHub Desktop.
Simple Stack and Set DataStructures - Example for CodeAfrique Students
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 Stack { | |
constructor() { | |
this.stack = []; | |
} | |
push(item) { | |
this.stack.unshift(item); | |
} | |
pop() { | |
this.stack = this.stack.slice(1, ); | |
} | |
peek() { | |
return this.stack[0]; | |
} | |
size() { | |
return this.stack.length; | |
} | |
}; | |
const stack = new Stack() | |
stack.size() | |
class Set { | |
constructor() { | |
this.collection = []; | |
} | |
hasItem(item) { | |
return this.collection.includes(item); | |
} | |
values() { | |
return this.collection.forEach(item => console.log(item)); | |
// return this.collection.toString(); | |
} | |
_addItemToCollection(item) { | |
this.collection.push(item); | |
return true; | |
} | |
add(item) { | |
this.hasItem(item) ? false : this._addItemToCollection(item); | |
} | |
_removeItemFromCollection(item) { | |
this.collection = this.collection.filter(i => i != item); | |
return true; | |
} | |
remove(item) { | |
this.hasItem(item) ? this._removeItemFromCollection(item) : false; | |
} | |
} | |
const colSet = new Set(); | |
colSet.add("item") | |
colSet.collection |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment