Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d97dbf5

Browse files
add solution in JS for 0225_implement_stack_using_queues.js
1 parent 6f1d93b commit d97dbf5

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+
/**
2+
* @description Build stack as an empty array
3+
*/
4+
const MyStack = function () {
5+
this.stack = [];
6+
};
7+
8+
/**
9+
* @description Pushes element to the top of the stack
10+
* @param {Number} value
11+
* @return {Void}
12+
*/
13+
MyStack.prototype.push = function (value) {
14+
this.stack.push(value);
15+
};
16+
17+
/**
18+
* @description Takes element out of the stack and return it
19+
* @return {Number}
20+
*/
21+
MyStack.prototype.pop = function () {
22+
const lastElement = this.stack.pop();
23+
return lastElement;
24+
};
25+
26+
/**
27+
* @description Returns the element on the top of the stack
28+
* @return {Number}
29+
*/
30+
MyStack.prototype.top = function () {
31+
const lastElement = this.stack[this.stack.length - 1];
32+
return lastElement;
33+
};
34+
35+
/**
36+
* @description Check if the stack is empty
37+
* @return {Boolean}
38+
*/
39+
MyStack.prototype.empty = function () {
40+
return this.stack.length === 0;
41+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /