Last active
August 29, 2015 14:27
-
-
Save niisar/8e53b5d79d34e72240ce to your computer and use it in GitHub Desktop.
Stack Class Implementation
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
function Stack(){ | |
this.dataStore = []; | |
this.top =0; | |
this.push = push; | |
this.pop = pop; | |
this.peek = peek; | |
this.length = length; | |
this.clear = clear; | |
} | |
// push | |
function push(element){ | |
this.dataStore[this.top++] = element; | |
} | |
//pop | |
function pop(element){ | |
return this.dataStore[--this.top]; | |
} | |
//peek | |
function peek(){ | |
return this.dataStore[this.top-1]; | |
} | |
//length | |
function length(){ | |
return this.top; | |
} | |
//clear | |
function clear(){ | |
this.top = 0; | |
} | |
// var s = new Stack(); | |
// s.push("Ram"); | |
// s.push("Shyam"); | |
// s.push("Katrina"); | |
// console.log("Item in stack is "+s.length()); | |
// console.log(s.peek()); | |
// var popped = s.pop(); | |
// console.log("Item in stack is "+s.length()); | |
// console.log(s.peek()); | |
// s.push("Neha"); | |
// console.log("Item in stack is "+s.length()); | |
// console.log(s.peek()); | |
// s.clear(); | |
// console.log("Item in stack is "+s.length()); | |
// console.log(s.peek()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment