-
Notifications
You must be signed in to change notification settings - Fork 42
/
solution.ts
167 lines (152 loc) · 3.96 KB
/
solution.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
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
class DoubleLinkedList {
public val:number;
public prev:DoubleLinkedList|null;
public next:DoubleLinkedList|null;
constructor (val:number) {
this.val = val;
this.prev = null;
this.next = null;
}
}
function insertAfter (prev:DoubleLinkedList, node:DoubleLinkedList) {
node.next = prev.next;
(node.next!).prev = node;
prev.next = node;
node.prev = prev;
}
function removeAfter (node:DoubleLinkedList) {
const next = node.next;
node.next = next.next;
(node.next!).prev = node;
next.prev = null;
next.next = null;
}
class FrontMiddleBackQueue {
public head:DoubleLinkedList;
public tail:DoubleLinkedList;
public middle:DoubleLinkedList|null;
public size:number;
constructor () {
this.head = new DoubleLinkedList(0);
this.tail = new DoubleLinkedList(0);
this.middle = null;
this.size = 0;
}
pushEmpty (node:DoubleLinkedList) {
this.head.next = node;
node.prev = this.head;
node.next = this.tail;
this.tail.prev = node;
this.middle = node;
}
pushFront (val: number): void {
const node = new DoubleLinkedList(val);
const size = this.size;
this.size++;
if (size === 0) {
this.pushEmpty(node);
return;
}
insertAfter(this.head, node);
if (size & 1) {
this.middle = this.middle.prev;
}
}
pushMiddle (val: number): void {
const node = new DoubleLinkedList(val);
const size = this.size;
this.size++;
if (size === 0) {
this.pushEmpty(node);
return;
}
if (size & 1) {
insertAfter(this.middle.prev, node);
this.middle = this.middle.prev;
} else {
insertAfter(this.middle, node);
this.middle = this.middle.next;
}
}
pushBack (val: number): void {
const node = new DoubleLinkedList(val);
const size = this.size;
this.size++;
if (size === 0) {
this.pushEmpty(node);
return;
}
insertAfter(this.tail.prev, node);
if (size % 2 === 0) {
this.middle = this.middle.next;
}
}
popEmpty ():number {
const val = (this.middle!).val;
this.middle = null;
this.head.next = this.tail;
this.tail.prev = this.head;
return val;
}
popFront (): number {
const size = this.size;
if (size === 0) {
return -1;
}
this.size--;
if (size === 1) {
return this.popEmpty();
}
const val = this.head.next.val;
if (size % 2 === 0) {
this.middle = this.middle.next;
}
removeAfter(this.head);
return val;
}
popMiddle (): number {
const size = this.size;
if (size === 0) {
return -1;
}
this.size--;
if (size === 1) {
return this.popEmpty();
}
const val = this.middle.val;
const prev = this.middle.prev;
if (size & 1) {
this.middle = this.middle.prev;
} else {
this.middle = this.middle.next;
}
removeAfter(prev);
return val;
}
popBack (): number {
const size = this.size;
if (size === 0) {
return -1;
}
this.size--;
if (size === 1) {
return this.popEmpty();
}
const val = this.tail.prev.val;
removeAfter(this.tail.prev.prev);
if (size & 1) {
this.middle = this.middle.prev;
}
return val;
}
}
/**
* Your FrontMiddleBackQueue object will be instantiated and called as such:
* var obj = new FrontMiddleBackQueue()
* obj.pushFront(val)
* obj.pushMiddle(val)
* obj.pushBack(val)
* var param_4 = obj.popFront()
* var param_5 = obj.popMiddle()
* var param_6 = obj.popBack()
*/