Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions Data Structures/Stack/Stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,52 @@
// Functions: push, pop, peek, view, length

//Creates a stack constructor
var Stack = function () {
//The top of the Stack
this.top=0;
//The array representation of the stack
this.stack = new Array();
}
var Stack = (function () {

function Stack() {
//The top of the Stack
this.top = 0;
//The array representation of the stack
this.stack = new Array();
}

//Adds a value onto the end of the stack
Stack.prototype.push=function(value) {
this.stack[this.top]=value;
Stack.prototype.push = function (value) {
this.stack[this.top] = value;
this.top++;
}
};

//Removes and returns the value at the end of the stack
Stack.prototype.pop = function(){
if(this.top === 0){
Stack.prototype.pop = function () {
if (this.top === 0) {
return "Stack is Empty";
}

this.top--;
var result = this.stack[this.top];
delete this.stack[this.top];
return result;
}
};

//Returns the size of the stack
Stack.prototype.size = function(){
Stack.prototype.size = function () {
return this.top;
}
};

//Returns the value at the end of the stack
Stack.prototype.peek = function(){
return this.stack[this.top-1];
Stack.prototype.peek = function () {
return this.stack[this.top - 1];
}

//To see all the elements in the stack
Stack.prototype.view= function(){
for(var i=0;i<this.top;i++)
Stack.prototype.view = function () {
for (var i = 0; i < this.top; i++)
console.log(this.stack[i]);
}
};

return Stack;

}());

//Implementation
var myStack = new Stack();
Expand Down