Skip to content

Commit 5931e37

Browse files
author
Christian Bender
committed
wrote prototype notation
1 parent b9d749a commit 5931e37

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

Data Structures/Stack/Stack.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@
77

88
// Functions: push, pop, peek, view, length
99

10-
//Creates a stack
10+
//Creates a stack constructor
1111
var Stack = function () {
1212
//The top of the Stack
1313
this.top=0;
1414
//The array representation of the stack
15-
this.stack = {};
15+
this.stack = new Array();
16+
}
1617

1718
//Adds a value onto the end of the stack
18-
this.push=function(value) {
19+
Stack.prototype.push=function(value) {
1920
this.stack[this.top]=value;
2021
this.top++;
2122
}
2223

2324
//Removes and returns the value at the end of the stack
24-
this.pop = function(){
25+
Stack.prototype.pop = function(){
2526
if(this.top === 0){
2627
return "Stack is Empty";
2728
}
@@ -33,21 +34,20 @@ var Stack = function () {
3334
}
3435

3536
//Returns the size of the stack
36-
this.size = function(){
37+
Stack.prototype.size = function(){
3738
return this.top;
3839
}
3940

4041
//Returns the value at the end of the stack
41-
this.peek = function(){
42+
Stack.prototype.peek = function(){
4243
return this.stack[this.top-1];
4344
}
4445

4546
//To see all the elements in the stack
46-
this.view= function(){
47+
Stack.prototype.view= function(){
4748
for(var i=0;i<this.top;i++)
4849
console.log(this.stack[i]);
4950
}
50-
}
5151

5252
//Implementation
5353
var myStack = new Stack();

0 commit comments

Comments
 (0)