-
Notifications
You must be signed in to change notification settings - Fork 0
/
queueUsingLL.ts
89 lines (79 loc) · 1.75 KB
/
queueUsingLL.ts
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
class QueueNodeObj {
value: number;
next: QueueNodeObj | null = null;
prev: QueueNodeObj | null = null;
constructor(value: number) {
this.value = value;
}
}
class QueueUsingLL {
pushPointer: QueueNodeObj | null = null;
popPointer: QueueNodeObj | null = null;
length = 0;
maxLength = 0;
constructor(k: number) {
this.maxLength = k;
}
enQueue(value: number): boolean {
if (this.isFull()) {
return false;
}
const node = new QueueNodeObj(value);
if (this.length === 0) {
this.pushPointer = node;
this.popPointer = node;
} else {
if (this.pushPointer) {
this.pushPointer.prev = node;
this.pushPointer = node;
node.next = this.pushPointer;
}
}
this.length++;
return true;
}
deQueue(): boolean {
if (this.length === 0) {
return false;
}
const value = this.popPointer;
if (value) {
value.next = null;
this.popPointer = value.prev;
value.prev = null;
}
if (this.popPointer) {
this.popPointer.next = null;
}
this.length--;
return true;
}
Front(): number {
if (this.length === 0) {
return -1;
}
return this.popPointer?.value ?? -1;
}
Rear(): number {
if (this.length === 0) {
return -1;
}
return this.pushPointer?.value ?? -1;
}
isEmpty(): boolean {
return this.length === 0;
}
isFull(): boolean {
return this.length >= this.maxLength;
}
}
/**
* Your MyCircularQueue object will be instantiated and called as such:
* var obj = new MyCircularQueue(k)
* var param_1 = obj.enQueue(value)
* var param_2 = obj.deQueue()
* var param_3 = obj.Front()
* var param_4 = obj.Rear()
* var param_5 = obj.isEmpty()
* var param_6 = obj.isFull()
*/