Stack implementation using VanillaJS
Stack is a very popular data structure. It is present as library classes/interfaces in many object oriented language like Java, C#. In JavaScript there is no stack data structure by default but we can create one. I have written following code which can be used to utilize Stack features in JavaScript development.
import UVModal from './UVModal';
var Stack = function(){
var items = [];
/**
* @description Pushes an item onto the top of this stack.
* @param {any} item - the item to be pushed onto this stack.
*/
function push(item) {
items.push(item);
}
/**
* @description Removes the object at the top of this stack and returns that object
* @return {any} - The object at the top of this stack
*/
function pop() {
return items.pop();
}
/**
* @description Looks at the object at the top of this stack without removing it from the stack.
* @return {any} - the object at the top of this stack
*/
function peek() {
return items[items.length-1];
}
/**
* @description Tests if this stack is empty.
* @return true if and only if this stack contains no items; false otherwise.
*/
function empty() {
return items.length === 0;
}
/**
* @description Search the item in the stack.
* @return {number} - Position of item from top, 1 if on top of stack, -1 if not present in stack
*/
function search(item) {
var index = items.indexOf(item);
if(index > -1) {
index += 1;
}
return index;
}
return {
push: push,
pop: pop,
peek: peek,
empty: empty,
search: search
};
};
var myStack = new Stack();
myStack.push('Yuvraj');
myStack.push('Patil');
console.info('Index of Patil: ', myStack.search('Patil'));
console.info('Top of Stack: ', myStack.peek());
myStack.pop();
console.info('Top of Stack: ', myStack.peek());
console.info('Is Stack Empty: ', myStack.empty());
import UVModal from './UVModal';
var Stack = function(){
var items = [];
/**
* @description Pushes an item onto the top of this stack.
* @param {any} item - the item to be pushed onto this stack.
*/
function push(item) {
items.push(item);
}
/**
* @description Removes the object at the top of this stack and returns that object
* @return {any} - The object at the top of this stack
*/
function pop() {
return items.pop();
}
/**
* @description Looks at the object at the top of this stack without removing it from the stack.
* @return {any} - the object at the top of this stack
*/
function peek() {
return items[items.length-1];
}
/**
* @description Tests if this stack is empty.
* @return true if and only if this stack contains no items; false otherwise.
*/
function empty() {
return items.length === 0;
}
/**
* @description Search the item in the stack.
* @return {number} - Position of item from top, 1 if on top of stack, -1 if not present in stack
*/
function search(item) {
var index = items.indexOf(item);
if(index > -1) {
index += 1;
}
return index;
}
return {
push: push,
pop: pop,
peek: peek,
empty: empty,
search: search
};
};
var myStack = new Stack();
myStack.push('Yuvraj');
myStack.push('Patil');
console.info('Index of Patil: ', myStack.search('Patil'));
console.info('Top of Stack: ', myStack.peek());
myStack.pop();
console.info('Top of Stack: ', myStack.peek());
console.info('Is Stack Empty: ', myStack.empty());
Comments
Post a Comment