Created
September 6, 2016 17:58
-
-
Save hatemcode/b6ff8e3b720097203cd8a40c51399756 to your computer and use it in GitHub Desktop.
Javascript Stack
This file contains 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
/** | |
* @Javascript Stack | |
* @author Hatem A. <hatem@tuta.io> | |
*/ | |
var Stack = function() { | |
var elements = []; | |
var top = 0; | |
var push = function(element){ | |
elements[top++] = element; | |
}; | |
var pop = function() { | |
return elements[--top]; | |
}; | |
var peek = function() { | |
return elements[top-1]; | |
}; | |
var length = function() { | |
return top; | |
}; | |
return {push:push, | |
pop:pop, | |
peek:peek, | |
length:length | |
}; | |
}; | |
var stack = new Stack(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment