Skip to content

Commit 5d05faf

Browse files
authored
Create 232-implement-queue-using-stacks.js
1 parent 5765506 commit 5d05faf

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

232-implement-queue-using-stacks.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Initialize your data structure here.
3+
*/
4+
const MyQueue = function() {
5+
this.queue = []
6+
};
7+
8+
/**
9+
* Push element x to the back of queue.
10+
* @param {number} x
11+
* @return {void}
12+
*/
13+
MyQueue.prototype.push = function(x) {
14+
this.queue.push(x)
15+
};
16+
17+
/**
18+
* Removes the element from in front of queue and returns that element.
19+
* @return {number}
20+
*/
21+
MyQueue.prototype.pop = function() {
22+
return this.queue.shift()
23+
};
24+
25+
/**
26+
* Get the front element.
27+
* @return {number}
28+
*/
29+
MyQueue.prototype.peek = function() {
30+
return this.queue[0]
31+
};
32+
33+
/**
34+
* Returns whether the queue is empty.
35+
* @return {boolean}
36+
*/
37+
MyQueue.prototype.empty = function() {
38+
return this.queue.length === 0
39+
};
40+
41+
/**
42+
* Your MyQueue object will be instantiated and called as such:
43+
* var obj = Object.create(MyQueue).createNew()
44+
* obj.push(x)
45+
* var param_2 = obj.pop()
46+
* var param_3 = obj.peek()
47+
* var param_4 = obj.empty()
48+
*/

0 commit comments

Comments
 (0)