mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-07 02:05:08 +08:00
Merge pull request #46 from christianbender/changed_stack
correspond to the target code of typescript
This commit is contained in:
@ -8,7 +8,9 @@
|
|||||||
// Functions: push, pop, peek, view, length
|
// Functions: push, pop, peek, view, length
|
||||||
|
|
||||||
//Creates a stack constructor
|
//Creates a stack constructor
|
||||||
var Stack = function () {
|
var Stack = (function () {
|
||||||
|
|
||||||
|
function Stack() {
|
||||||
//The top of the Stack
|
//The top of the Stack
|
||||||
this.top = 0;
|
this.top = 0;
|
||||||
//The array representation of the stack
|
//The array representation of the stack
|
||||||
@ -19,7 +21,7 @@ var Stack = function () {
|
|||||||
Stack.prototype.push = function (value) {
|
Stack.prototype.push = function (value) {
|
||||||
this.stack[this.top] = value;
|
this.stack[this.top] = value;
|
||||||
this.top++;
|
this.top++;
|
||||||
}
|
};
|
||||||
|
|
||||||
//Removes and returns the value at the end of the stack
|
//Removes and returns the value at the end of the stack
|
||||||
Stack.prototype.pop = function () {
|
Stack.prototype.pop = function () {
|
||||||
@ -31,12 +33,12 @@ var Stack = function () {
|
|||||||
var result = this.stack[this.top];
|
var result = this.stack[this.top];
|
||||||
delete this.stack[this.top];
|
delete this.stack[this.top];
|
||||||
return result;
|
return result;
|
||||||
}
|
};
|
||||||
|
|
||||||
//Returns the size of the stack
|
//Returns the size of the stack
|
||||||
Stack.prototype.size = function () {
|
Stack.prototype.size = function () {
|
||||||
return this.top;
|
return this.top;
|
||||||
}
|
};
|
||||||
|
|
||||||
//Returns the value at the end of the stack
|
//Returns the value at the end of the stack
|
||||||
Stack.prototype.peek = function () {
|
Stack.prototype.peek = function () {
|
||||||
@ -47,7 +49,11 @@ var Stack = function () {
|
|||||||
Stack.prototype.view = function () {
|
Stack.prototype.view = function () {
|
||||||
for (var i = 0; i < this.top; i++)
|
for (var i = 0; i < this.top; i++)
|
||||||
console.log(this.stack[i]);
|
console.log(this.stack[i]);
|
||||||
}
|
};
|
||||||
|
|
||||||
|
return Stack;
|
||||||
|
|
||||||
|
}());
|
||||||
|
|
||||||
//Implementation
|
//Implementation
|
||||||
var myStack = new Stack();
|
var myStack = new Stack();
|
||||||
|
Reference in New Issue
Block a user