Created
June 16, 2014 18:04
-
-
Save bschandramohan/a9dcd9fe70b88ccbb417 to your computer and use it in GitHub Desktop.
Implementation of a Stack in JS
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
| /** | |
| * Implementation of a Stack in Javascript | |
| * @constructor | |
| * @author Chandra Mohan | |
| */ | |
| function Stack() { | |
| /** | |
| * Element to store data. _ is used to imply that clients should not modify it. | |
| * @type {Array} | |
| * @private | |
| */ | |
| this._storeArray = []; | |
| /** | |
| * Index of the array denoting top position. _ is used to imply that clients should not modify it. | |
| * @type {number} | |
| * @private | |
| */ | |
| this._index = -1; | |
| } | |
| // Now add Prototype methods. | |
| Stack.prototype = { | |
| /** | |
| * Utility method to return _index. Note _index is denoted to be private. But so are these methods. | |
| * @returns {number} | |
| * @private | |
| */ | |
| _getIndex: function () { | |
| return this._index; | |
| }, | |
| _setIndex : function (indexValue) { | |
| this._index = indexValue; | |
| }, | |
| _getData : function () { | |
| return this._storeArray[this._index]; | |
| }, | |
| _addData : function (value) { | |
| this._storeArray[this._index] = value | |
| }, | |
| /** | |
| * Push an element into Stack | |
| * @param val | |
| */ | |
| push : function (val) { | |
| this._setIndex(this._getIndex() + 1); | |
| this._addData(val); | |
| console.log("Pushed: " + val); | |
| }, | |
| /** | |
| * Pop an element out of Stack | |
| * @returns {null} | |
| */ | |
| pop : function () { | |
| if (this._getIndex() < 0) { | |
| return null; | |
| } else { | |
| console.log("Popping: " + this._getData()); | |
| this._setIndex(this._getIndex() - 1); | |
| } | |
| }, | |
| /** | |
| * Check if the stack is empty. | |
| * @returns {boolean} | |
| */ | |
| isEmpty : function () { | |
| console.log("isEmpty === " + (this._getIndex() < 0) + " " + "Current Index=" + this._getIndex()); | |
| return this._getIndex() < 0; | |
| } | |
| } | |
| // TEST CODE | |
| var myStack = new Stack(); | |
| console.log("myStack: " + myStack); | |
| console.log("myStack.index: " + myStack._index); | |
| myStack.push(201); | |
| myStack.push(202); | |
| myStack.push(203); | |
| myStack.push(204); | |
| myStack.pop(); | |
| myStack.pop(); | |
| myStack.isEmpty(); | |
| myStack.pop(); | |
| myStack.pop(); | |
| myStack.isEmpty(); | |
| var myStack1 = new Stack(); | |
| console.log("myStack1: " + myStack1); | |
| myStack1.push(301); | |
| myStack1.push(302); | |
| myStack1.pop(); | |
| myStack1.push(303); | |
| myStack1.push(304); | |
| myStack1.pop(); | |
| myStack1.pop(); | |
| myStack1.isEmpty(); | |
| myStack1.pop(); | |
| myStack1.isEmpty(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment