Created
April 18, 2017 16:31
-
-
Save brianxautumn/d7f9a85c11f764630a9ec6a70835f099 to your computer and use it in GitHub Desktop.
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
class Stack{ | |
constructor(length){ | |
this.data = new Array(length); | |
this.pointer = 0; | |
} | |
push(value){ | |
if(this.pointer === this.data.length){ | |
throw "Overflow" | |
} | |
this.data[this.pointer] = value; | |
this.pointer++; | |
} | |
pop(){ | |
var value = this.data[this.pointer - 1]; | |
this.pointer--; | |
return value; | |
} | |
peek(){ | |
if(this.data.length > 0) | |
return this.data[this.pointer - 1]; | |
else | |
return null; | |
} | |
} | |
var stack = new Stack(5); | |
stack.push(1); | |
stack.push(2); | |
stack.push(3); | |
stack.push(4); | |
stack.push(5); | |
console.log(stack.peek()); | |
stack.pop(); | |
console.log(stack.peek()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment