From 52c60aba48d6760bc86fd753adcc573cc7ffe1a6 Mon Sep 17 00:00:00 2001 From: Manish Date: Tue, 22 Aug 2023 10:58:01 +1000 Subject: [PATCH] Data Structures: Create a Stack Class --- Data Structures/stackClass.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Data Structures/stackClass.js diff --git a/Data Structures/stackClass.js b/Data Structures/stackClass.js new file mode 100644 index 0000000..2281834 --- /dev/null +++ b/Data Structures/stackClass.js @@ -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 +}