Skip to content

Commit fe4b125

Browse files
author
hasibulislam999
committed
Design Front Middle Back Queue problem solved
1 parent aad1282 commit fe4b125

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Title: Design Front Middle Back Queue
3+
* Description: Design a queue that supports push and pop operations in the front, middle, and back.
4+
* Author: Hasibul Islam
5+
* Date: 28/04/2023
6+
*/
7+
8+
var FrontMiddleBackQueue = function () {
9+
this.queue = [];
10+
};
11+
12+
/**
13+
* @param {number} val
14+
* @return {void}
15+
*/
16+
FrontMiddleBackQueue.prototype.pushFront = function (val) {
17+
this.queue.unshift(val);
18+
};
19+
20+
/**
21+
* @param {number} val
22+
* @return {void}
23+
*/
24+
FrontMiddleBackQueue.prototype.pushMiddle = function (val) {
25+
let middle = Math.floor(this.queue.length / 2);
26+
this.queue.splice(middle, 0, val);
27+
};
28+
29+
/**
30+
* @param {number} val
31+
* @return {void}
32+
*/
33+
FrontMiddleBackQueue.prototype.pushBack = function (val) {
34+
this.queue.push(val);
35+
};
36+
37+
/**
38+
* @return {number}
39+
*/
40+
FrontMiddleBackQueue.prototype.popFront = function () {
41+
if (!this.queue.length) return -1;
42+
return this.queue.shift();
43+
};
44+
45+
/**
46+
* @return {number}
47+
*/
48+
FrontMiddleBackQueue.prototype.popMiddle = function () {
49+
if (!this.queue.length) return -1;
50+
let middle = Math.floor(this.queue.length / 2);
51+
if (this.queue.length % 2 == 0) middle--;
52+
return this.queue.splice(middle, 1);
53+
};
54+
55+
/**
56+
* @return {number}
57+
*/
58+
FrontMiddleBackQueue.prototype.popBack = function () {
59+
if (!this.queue.length) return -1;
60+
return this.queue.pop();
61+
};
62+
63+
/**
64+
* Your FrontMiddleBackQueue object will be instantiated and called as such:
65+
* var obj = new FrontMiddleBackQueue()
66+
* obj.pushFront(val)
67+
* obj.pushMiddle(val)
68+
* obj.pushBack(val)
69+
* var param_4 = obj.popFront()
70+
* var param_5 = obj.popMiddle()
71+
* var param_6 = obj.popBack()
72+
*/

0 commit comments

Comments
 (0)