niisar
8/19/2015 - 12:39 PM

Stack Class Implementation

Stack Class Implementation

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());