Data Structures: Create a Stack Class
This commit is contained in:
parent
8c899a5efb
commit
52c60aba48
24
Data Structures/stackClass.js
Normal file
24
Data Structures/stackClass.js
Normal file
@ -0,0 +1,24 @@
|
||||
// https://www.freecodecamp.org/learn/coding-interview-prep/data-structures/create-a-stack-class
|
||||
function Stack() {
|
||||
var collection = [];
|
||||
this.print = function () {
|
||||
console.log(collection);
|
||||
};
|
||||
// Only change code below this line
|
||||
this.push = function (elem) {
|
||||
collection.push(elem);
|
||||
};
|
||||
this.pop = function () {
|
||||
return collection.pop();
|
||||
};
|
||||
this.peek = function () {
|
||||
return collection[collection.length - 1];
|
||||
};
|
||||
this.isEmpty = function () {
|
||||
return !collection.length;
|
||||
};
|
||||
this.clear = function () {
|
||||
collection = [];
|
||||
};
|
||||
// Only change code above this line
|
||||
}
|
Loading…
Reference in New Issue
Block a user