Skip to content

Instantly share code, notes, and snippets.

@arrbxr
Created August 27, 2018 19:46
Show Gist options
  • Save arrbxr/7ae36064b413d678f53941b5fe19c449 to your computer and use it in GitHub Desktop.
Save arrbxr/7ae36064b413d678f53941b5fe19c449 to your computer and use it in GitHub Desktop.
stack of javascript created by arrbxr - https://repl.it/@arrbxr/stack-of-javascript
// Show the complete implementation
// of the stack class
function Stack() {
this.dataStore = [];
this.top = 0;
this.push = push;
this.pop = pop;
this.peek = peek;
this.clear = clear;
this.length = length;
}
// push function
function push(element) {
this.dataStore[this.top++] = element;
}
// peek function
function peek() {
return this.dataStore[this.top - 1];
}
// pop function
function pop() {
return this.dataStore[--this.top];
}
// clear function
function clear() {
return this.top = 0;
}
// length function
function length() {
return this.top;
}
var s = new Stack();
s.push("ravi");
s.push("raj");
s.push('Abhishek');
console.log("Lenght = " + s.length());
console.log(s.peek());
var popped = s.pop();
console.log("The Popped Element is: " + popped);
console.log(s.peek());
s.push('simpee');
console.log(s.peek());
s.clear();
console.log("length: " + s.length());
console.log(s.peek());
s.push("Pagla");
console.log(s.peek());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment