-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path622.java
More file actions
170 lines (142 loc) · 4.17 KB
/
622.java
File metadata and controls
170 lines (142 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
__________________________________________________________________________________________________
sample 44 ms submission
class MyCircularQueue {
int[] array;
int head;
int tail;
int size;
boolean isFull;
boolean isEmpty;
/** Initialize your data structure here. Set the size of the queue to be k. */
public MyCircularQueue(int k) {
array = new int[k];
head = -1;
tail = -1;
size = k;
isFull = false;
isEmpty = true;
}
/** Insert an element into the circular queue. Return true if the operation is successful. */
public boolean enQueue(int value) {
if (isFull) return false;
if (isEmpty) isEmpty = false;
head = (head+1)%size;
array[head] = value;
if ((head+1)%size==(tail+1)%size) isFull = true;
return true;
}
/** Delete an element from the circular queue. Return true if the operation is successful. */
public boolean deQueue() {
if (isEmpty) return false;
if (isFull) isFull = false;
tail = (tail+1)%size;
array[tail] = -1;
if (head==tail) {tail = -1; head = -1; isEmpty = true;}
return true;
}
/** Get the front item from the queue. */
public int Front() {
if (isEmpty) return -1;
return array[(tail+1)%size];
}
/** Get the last item from the queue. */
public int Rear() {
if (isEmpty) return -1;
return array[(head)%size];
}
/** Checks whether the circular queue is empty or not. */
public boolean isEmpty() {
return isEmpty;
}
/** Checks whether the circular queue is full or not. */
public boolean isFull() {
return isFull;
}
}
/**
* Your MyCircularQueue object will be instantiated and called as such:
* MyCircularQueue obj = new MyCircularQueue(k);
* boolean param_1 = obj.enQueue(value);
* boolean param_2 = obj.deQueue();
* int param_3 = obj.Front();
* int param_4 = obj.Rear();
* boolean param_5 = obj.isEmpty();
* boolean param_6 = obj.isFull();
*/
__________________________________________________________________________________________________
sample 35952 kb submission
class MyCircularQueue {
int[] array;
int headIndex;
int tailIndex;
int size;
public MyCircularQueue(int size) {
array = new int[size];
headIndex = -1;
tailIndex = -1;
this.size = size;
}
/**
* Insert an element into the circular queue. Return true if the operation is successful.
*/
public boolean enQueue(int value) {
if (isFull()) {
System.out.println("Queue is full");
return false;
}
if (isEmpty()) {
headIndex = 0;
tailIndex = 0;
} else {
tailIndex = (tailIndex + 1) % size;
}
array[tailIndex] = value;
return true;
}
/**
* Delete an element from the circular queue. Return true if the operation is successful.
*/
public boolean deQueue() {
if (isEmpty()) {
System.out.println("Queue is empty");
return false;
}
array[headIndex] = 0;
if (headIndex == tailIndex) {
headIndex = -1;
tailIndex = -1;
return true;
}
headIndex = (headIndex + 1) % size;
return true;
}
/**
* Get the front item from the queue.
*/
public int Front() {
if (headIndex != -1)
return array[headIndex];
return -1;
}
/**
* Get the last item from the queue.
*/
public int Rear() {
if (tailIndex != -1)
return array[tailIndex];
return -1;
}
/**
* Checks whether the circular queue is empty or not.
*/
public boolean isEmpty() {
return tailIndex == -1 & headIndex == -1;
}
/**
* Checks whether the circular queue is full or not.
*/
public boolean isFull() {
return (tailIndex + 1) % size == headIndex;
}
}
__________________________________________________________________________________________________