Skip to content

Commit 5136e4d

Browse files
committed
No frills implementation of a stack.
1 parent b94c46e commit 5136e4d

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var stack = function(){
2+
this.stackArray = [];
3+
4+
this.push = function(num){
5+
this.stackArray.push(num);
6+
}
7+
8+
this.pop = function(){
9+
if(this.stackArray.length==0){
10+
return "Error: No elements left in array";
11+
}
12+
return this.stackArray.pop();
13+
}
14+
15+
this.peek = function(){
16+
if(this.stackArray.length==0){
17+
return "Error: No elements present in array";
18+
}
19+
return this.stackArray[this.stackArray.length-1];
20+
}
21+
this.isEmpty = function(){
22+
if(this.stackArray.length==0){
23+
return true;
24+
}
25+
return false;
26+
}
27+
28+
this.printStack = function(){
29+
console.log(this.stackArray);
30+
}
31+
32+
}
33+
34+
var stackTest = new stack();
35+
stackTest.stackArray = [1,2,4];
36+
stackTest.push(5);
37+
stackTest.printStack();
38+
console.log("Peek: " + stackTest.peek());
39+
console.log("IsEmpty: " + stackTest.isEmpty());
40+
console.log("Popped: " + stackTest.pop());
41+

0 commit comments

Comments
 (0)