-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataStructure_Deque.js
57 lines (49 loc) · 1014 Bytes
/
DataStructure_Deque.js
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
class Deque {
constructor() {
this.list = {};
this.head = 0;
this.tail = 1;
}
pushFront(item) {
this.list[this.head] = item;
this.head--;
}
pushBack(item) {
this.list[this.tail] = item;
this.tail++;
}
popFront() {
let item = this.list[this.head + 1];
if (item === undefined) return null;
delete this.list[this.head + 1];
this.head++;
if (this.head === this.tail) this.tail++;
return item;
}
popBack() {
let item = this.list[this.tail - 1];
if (item === undefined) return null;
delete this.list[this.tail - 1];
this.tail--;
if (this.tail === this.head) this.head--;
return item;
}
getSize() {
return this.tail - this.head - 1;
}
isEmpty() {
let item = this.popBack();
if (item === null) {
return true;
} else {
this.pushBack(item);
return false;
}
}
peekFront() {
return this.list[this.head + 1];
}
peekBack() {
return this.list[this.tail - 1];
}
}