stack-op
Version:
Stack data structure implementation and basic operations
53 lines (43 loc) • 927 B
JavaScript
/**
* @Author : Vaibhav Page
* @Description : Provides implementation for Stack ADT
*/
var exports = module.exports = {};
class Stack {
constructor() {
this._top = -1;
this._stack = new Array();
return this;
}
getSize() {
return this._stack.length;
}
push(item) {
this._stack.push(item);
this._top++;
}
pop() {
if(this._top > -1) {
var item = this._stack.pop();
--this._top;
return item;
} else {
throw new Error('Stack is empty');
}
}
peek() {
if(this._top > -1) {
var item = this._stack[this._top];
return item;
} else {
throw new Error('Stack is empty');
}
}
}
/**
* API
*
*/
exports.createStack = function () {
return new Stack();
};