-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deque.ts
58 lines (47 loc) · 1.44 KB
/
Deque.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
import InvalidOperationError from './errors/InvalidOperationError.js';
import { LinkedList } from './index.js';
class Deque<T> {
private list: LinkedList<T>;
constructor(initialArray: T[] = []) {
this.list = new LinkedList<T>(initialArray);
}
public get size(): number {
return this.list.size;
}
public get empty(): boolean {
return this.list.size == 0;
}
public get front(): T {
if (this.list.size == 0) {
throw new InvalidOperationError('Unable to access element from Empty Deque');
}
return this.list.front;
}
public get back(): T {
if (this.list.size == 0) {
throw new InvalidOperationError('Unable to access element from Empty Deque');
}
return this.list.back;
}
public pushBack(value: T): number {
this.list.pushBack(value);
return this.list.size;
}
public pushFront(value: T): number {
this.list.pushFront(value);
return this.list.size;
}
public popBack(): T {
if (this.list.size == 0) {
throw new InvalidOperationError('Unable to pop element from Empty Deque');
}
return this.list.popBack();
}
public popFront(): T {
if (this.list.size == 0) {
throw new InvalidOperationError('Unable to pop element from Empty Deque');
}
return this.list.popFront();
}
}
export default Deque;