-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyCircularQueue.java
More file actions
73 lines (63 loc) · 1.48 KB
/
MyCircularQueue.java
File metadata and controls
73 lines (63 loc) · 1.48 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
package com.leetcode.list;
final class MyCircularQueue {
private final int maxSize;
private ListNode head;
private ListNode tail;
private int size = 0;
MyCircularQueue(int k) {
this.head = null;
this.tail = null;
this.maxSize = k;
}
public boolean enQueue(int value) {
if (!isFull()) {
addAtTail(value);
return true;
}
return false;
}
public boolean deQueue() {
if (!isEmpty()) {
if (head == tail) {
head = null;
tail = null;
} else {
head = head.next;
}
size--;
return true;
}
return false;
}
@SuppressWarnings( {"checkstyle:MethodName", "squid:S100"})
public int Front() {
if (head != null) {
return head.val;
}
return -1;
}
@SuppressWarnings( {"checkstyle:MethodName", "squid:S100"})
public int Rear() {
if (tail != null) {
return tail.val;
}
return -1;
}
public boolean isEmpty() {
return size == 0;
}
public boolean isFull() {
return size == maxSize;
}
private void addAtTail(int val) {
ListNode newNode = new ListNode(val);
if (tail == null) {
tail = newNode;
head = tail;
} else {
tail.next = newNode;
tail = newNode;
}
size++;
}
}