File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change 1+ /*Implement the following operations of a queue using stacks.
2+
3+ push(x) -- Push element x to the back of queue.
4+ pop() -- Removes the element from in front of queue.
5+ peek() -- Get the front element.
6+ empty() -- Return whether the queue is empty.*/
7+
8+ /**
9+ * Initialize your data structure here.
10+ */
11+ var MyQueue = function ( ) {
12+ this . q = [ ]
13+ } ;
14+
15+ /**
16+ * Push element x to the back of queue.
17+ * @param {number } x
18+ * @return {void }
19+ */
20+ MyQueue . prototype . push = function ( x ) {
21+ this . q . unshift ( x )
22+ } ;
23+
24+ /**
25+ * Removes the element from in front of queue and returns that element.
26+ * @return {number }
27+ */
28+ MyQueue . prototype . pop = function ( ) {
29+ return this . q . pop ( )
30+ } ;
31+
32+ /**
33+ * Get the front element.
34+ * @return {number }
35+ */
36+ MyQueue . prototype . peek = function ( ) {
37+ return this . q [ this . q . length - 1 ]
38+ } ;
39+
40+ /**
41+ * Returns whether the queue is empty.
42+ * @return {boolean }
43+ */
44+ MyQueue . prototype . empty = function ( ) {
45+ return this . q [ 0 ] == null
46+ } ;
47+
48+ /**
49+ * Your MyQueue object will be instantiated and called as such:
50+ * var obj = Object.create(MyQueue).createNew()
51+ * obj.push(x)
52+ * var param_2 = obj.pop()
53+ * var param_3 = obj.peek()
54+ * var param_4 = obj.empty()
55+ */
You can’t perform that action at this time.
0 commit comments