Skip to content

Instantly share code, notes, and snippets.

@hatemcode
Created September 6, 2016 17:58
Show Gist options
  • Save hatemcode/b6ff8e3b720097203cd8a40c51399756 to your computer and use it in GitHub Desktop.
Save hatemcode/b6ff8e3b720097203cd8a40c51399756 to your computer and use it in GitHub Desktop.
Javascript Stack
/**
* @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