1+ /**
2+ * Initialize your data structure here. Set the size of the deque to be k.
3+ * @param {number } k
4+ */
5+ var MyCircularDeque = function ( k ) {
6+ this . size = k ;
7+ this . data = [ ] ;
8+ } ;
9+
10+ /**
11+ * Adds an item at the front of Deque. Return true if the operation is successful.
12+ * @param {number } value
13+ * @return {boolean }
14+ */
15+ MyCircularDeque . prototype . insertFront = function ( value ) {
16+ if ( this . isFull ( ) ) return false ;
17+ this . data . unshift ( value ) ;
18+ return true ;
19+ } ;
20+
21+ /**
22+ * Adds an item at the rear of Deque. Return true if the operation is successful.
23+ * @param {number } value
24+ * @return {boolean }
25+ */
26+ MyCircularDeque . prototype . insertLast = function ( value ) {
27+ if ( this . isFull ( ) ) return false ;
28+ this . data . push ( value )
29+ return true ;
30+ } ;
31+
32+ /**
33+ * Deletes an item from the front of Deque. Return true if the operation is successful.
34+ * @return {boolean }
35+ */
36+ MyCircularDeque . prototype . deleteFront = function ( ) {
37+ if ( this . isEmpty ( ) ) return false ;
38+ this . data . shift ( ) ;
39+ return true ;
40+ } ;
41+
42+ /**
43+ * Deletes an item from the rear of Deque. Return true if the operation is successful.
44+ * @return {boolean }
45+ */
46+ MyCircularDeque . prototype . deleteLast = function ( ) {
47+ if ( this . isEmpty ( ) ) return false ;
48+ this . data . pop ( ) ;
49+ return true ;
50+ } ;
51+
52+ /**
53+ * Get the front item from the deque.
54+ * @return {number }
55+ */
56+ MyCircularDeque . prototype . getFront = function ( ) {
57+ if ( this . data . length === 0 ) return - 1 ;
58+ return this . data [ 0 ] ;
59+ } ;
60+
61+ /**
62+ * Get the last item from the deque.
63+ * @return {number }
64+ */
65+ MyCircularDeque . prototype . getRear = function ( ) {
66+ let length = this . data . length ;
67+ if ( length === 0 ) return - 1 ;
68+ return this . data [ length - 1 ] ;
69+ } ;
70+
71+ /**
72+ * Checks whether the circular deque is empty or not.
73+ * @return {boolean }
74+ */
75+ MyCircularDeque . prototype . isEmpty = function ( ) {
76+ return this . data . length === 0 ;
77+ } ;
78+
79+ /**
80+ * Checks whether the circular deque is full or not.
81+ * @return {boolean }
82+ */
83+ MyCircularDeque . prototype . isFull = function ( ) {
84+ return this . data . length === this . size ;
85+ } ;
86+
87+ /**
88+ * Your MyCircularDeque object will be instantiated and called as such:
89+ * var obj = new MyCircularDeque(k)
90+ * var param_1 = obj.insertFront(value)
91+ * var param_2 = obj.insertLast(value)
92+ * var param_3 = obj.deleteFront()
93+ * var param_4 = obj.deleteLast()
94+ * var param_5 = obj.getFront()
95+ * var param_6 = obj.getRear()
96+ * var param_7 = obj.isEmpty()
97+ * var param_8 = obj.isFull()
98+ */
0 commit comments