Home > JavaScript > Javascript Stack class

Javascript Stack class

January 8, 2010

Notice: This class is subject to change.

class Stack:


<script type="text/javascript">

/*[ Creates a new Stack. A Stack is a LIFO (last-in-first-out) data structure. ]*/
var Stack = function()
{
	/*[ Inner array ]*/
	var stack = [];

	/*[ Adds an element to the Stack ]*/
	this.add = function(element) {
		stack.push(element);
		return this;
	};

	/*[ Retrieve the last element from the Stack ]*/
	this.get = function() {
		return stack.pop();
	};

	/*[ Remove the first element from the Stack ]*/
	this.remove = function() {
		stack.pop();
		return this;
	};

	/*[ Retrieve the size of the Stack ]*/
	this.size = function() {
		return stack.length;
	};

	/*[ Check to see whether or not the Stack is empty ]*/
	this.isEmpty = function() {
		return (stack.length == 0);
	};

	/*[ Retrieve the Stack's content ]*/
	this.getStack = function() {
		return stack;
	};
};

</script>
Advertisements
Categories: JavaScript Tags: ,
%d bloggers like this: