1+ /*
2+ * 641. Design Circular Deque
3+ * 设计循环双端队列
4+ */
5+ public class LeetCode_641_569 {
6+
7+ public static void main (String [] args ) {
8+
9+ MyCircularDeque circularDeque = new MyCircularDeque (8 );
10+ circularDeque .insertFront (5 );
11+ circularDeque .getFront ();
12+ circularDeque .isEmpty ();
13+ circularDeque .deleteFront ();
14+ circularDeque .insertLast (3 );
15+ circularDeque .getRear ();
16+ circularDeque .insertLast (7 );
17+ circularDeque .insertFront (7 );
18+ circularDeque .deleteLast ();
19+ circularDeque .insertLast (4 );
20+ circularDeque .isEmpty ();
21+
22+
23+
24+
25+
26+
27+
28+ /*circularDeque.insertFront(1);
29+ circularDeque.insertLast(2);
30+ circularDeque.getFront();
31+ circularDeque.insertLast(3);
32+ circularDeque.getFront();
33+ circularDeque.insertFront(5);
34+ circularDeque.getRear();
35+ circularDeque.getFront();
36+ circularDeque.getFront();
37+ circularDeque.deleteLast();
38+ circularDeque.getRear();*/
39+
40+
41+ /* circularDeque.insertLast(1); // 返回 true
42+ circularDeque.insertLast(2); // 返回 true
43+ circularDeque.insertFront(3); // 返回 true
44+ circularDeque.insertFront(4); // 已经满了,返回 false
45+ circularDeque.getRear(); // 返回 2
46+ circularDeque.isFull(); // 返回 true
47+ circularDeque.deleteLast(); // 返回 true
48+ circularDeque.insertFront(4); // 返回 true
49+ circularDeque.getFront(); // 返回 4
50+ */
51+ }
52+
53+
54+ }
55+
56+ class MyCircularDeque {
57+
58+ class Node {
59+ Node prev ;
60+ int value ;
61+ Node next ;
62+
63+ Node ( Node prev , int value , Node next ) {
64+ this .prev = prev ;
65+ this .value = value ;
66+ this .next = next ;
67+ }
68+ }
69+
70+ int maxSize ;
71+ int size ;
72+ Node head ;
73+ Node tail ;
74+
75+ public MyCircularDeque (int k ) {
76+ this .maxSize = k ;
77+ }
78+
79+ public boolean insertFront (int value ) {
80+ if ( size == maxSize )
81+ return false ;
82+ Node n = new Node ( null , value , head );
83+ if ( head == null )
84+ tail = n ;
85+ else
86+ head .prev = n ;
87+ head = n ;
88+ size ++;
89+ return true ;
90+ }
91+
92+ public boolean insertLast (int value ) {
93+ if ( size == maxSize )
94+ return false ;
95+ Node n = new Node ( tail , value , null );
96+ if ( tail == null )
97+ head = n ;
98+ else
99+ tail .next = n ;
100+ tail = n ;
101+ size ++;
102+ return true ;
103+ }
104+
105+ public boolean deleteFront () {
106+ if ( head == null )
107+ return false ;
108+ Node n = head ;
109+ Node next = head .next ;
110+ n .next = null ;
111+ head = next ;
112+ if ( head != null )
113+ head .prev = null ;
114+ else
115+ tail = null ;
116+ size --;
117+ return true ;
118+ }
119+
120+ public boolean deleteLast () {
121+ if ( tail == null )
122+ return false ;
123+ Node n = tail ;
124+ Node prev = tail .prev ;
125+ n .prev = null ;
126+ tail = prev ;
127+ if ( tail != null )
128+ tail .next = null ;
129+ else
130+ head = null ;
131+ size --;
132+ return true ;
133+ }
134+
135+ public int getFront () {
136+ return (size ==0 ) ? -1 : head .value ;
137+ }
138+
139+ public int getRear () {
140+ return (size ==0 ) ? -1 : tail .value ;
141+ }
142+
143+ public boolean isEmpty () {
144+ return (size == 0 );
145+ }
146+
147+ public boolean isFull () {
148+ return (size == maxSize );
149+ }
150+ }
0 commit comments