Skip to content

Commit ef59b55

Browse files
author
Christian Bender
authored
Merge pull request TheAlgorithms#46 from christianbender/changed_stack
correspond to the target code of typescript
2 parents 10650f3 + fb08370 commit ef59b55

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

Data Structures/Stack/Stack.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,52 @@
88
// Functions: push, pop, peek, view, length
99

1010
//Creates a stack constructor
11-
var Stack = function () {
12-
//The top of the Stack
13-
this.top=0;
14-
//The array representation of the stack
15-
this.stack = new Array();
16-
}
11+
var Stack = (function () {
12+
13+
function Stack() {
14+
//The top of the Stack
15+
this.top = 0;
16+
//The array representation of the stack
17+
this.stack = new Array();
18+
}
1719

1820
//Adds a value onto the end of the stack
19-
Stack.prototype.push=function(value) {
20-
this.stack[this.top]=value;
21+
Stack.prototype.push = function (value) {
22+
this.stack[this.top] = value;
2123
this.top++;
22-
}
24+
};
2325

2426
//Removes and returns the value at the end of the stack
25-
Stack.prototype.pop = function(){
26-
if(this.top === 0){
27+
Stack.prototype.pop = function () {
28+
if (this.top === 0) {
2729
return "Stack is Empty";
2830
}
2931

3032
this.top--;
3133
var result = this.stack[this.top];
3234
delete this.stack[this.top];
3335
return result;
34-
}
36+
};
3537

3638
//Returns the size of the stack
37-
Stack.prototype.size = function(){
39+
Stack.prototype.size = function () {
3840
return this.top;
39-
}
41+
};
4042

4143
//Returns the value at the end of the stack
42-
Stack.prototype.peek = function(){
43-
return this.stack[this.top-1];
44+
Stack.prototype.peek = function () {
45+
return this.stack[this.top - 1];
4446
}
4547

4648
//To see all the elements in the stack
47-
Stack.prototype.view= function(){
48-
for(var i=0;i<this.top;i++)
49+
Stack.prototype.view = function () {
50+
for (var i = 0; i < this.top; i++)
4951
console.log(this.stack[i]);
50-
}
52+
};
53+
54+
return Stack;
55+
56+
}());
5157

5258
//Implementation
5359
var myStack = new Stack();

0 commit comments

Comments
 (0)