-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.ts
145 lines (114 loc) · 2.93 KB
/
index.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
import { LinkedList, LinkedListNode } from "../linked-list/index.ts";
export interface DoublyLinkedListNode<T> extends LinkedListNode<T> {
previous?: DoublyLinkedListNode<T>;
next?: DoublyLinkedListNode<T>;
value: T;
}
export class DoublyLinkedListNode<T> extends LinkedListNode<T>
implements DoublyLinkedListNode<T> {
previous?: DoublyLinkedListNode<T>;
next?: DoublyLinkedListNode<T>;
constructor(
value: T,
next?: DoublyLinkedListNode<T>,
previous?: DoublyLinkedListNode<T>
) {
super(value, next);
this.previous = previous;
}
}
export interface DoublyLinkedList<T> extends LinkedList<T> {
head?: DoublyLinkedListNode<T>;
tail?: DoublyLinkedListNode<T>;
length: number;
}
export class DoublyLinkedList<T> extends LinkedList<T>
implements DoublyLinkedList<T> {
head?: DoublyLinkedListNode<T>;
tail?: DoublyLinkedListNode<T>;
length = 0;
clear() {
console.log("clearing here...");
super.clear();
this.tail = undefined;
}
addBefore(n1: DoublyLinkedListNode<T>, n2: DoublyLinkedListNode<T>) {
n2.next = n1;
if (!n1.previous) {
this.head = n2;
} else {
n2.previous = n1.previous;
n1.previous.next = n2;
}
n1.previous = n2;
this.length++;
return n2;
}
addAfter(n1: DoublyLinkedListNode<T>, n2: DoublyLinkedListNode<T>) {
n2.previous = n1;
if (!n1.next) {
this.tail = n2;
} else {
n2.next = n1.next;
n1.next.previous = n2;
}
n1.next = n2;
this.length++;
return n2;
}
addHead(value: T) {
const node = new DoublyLinkedListNode(value);
if (!this.head) {
this.head = node;
this.tail = node;
} else {
this.head = this.addBefore(this.head, node);
}
return this.head;
}
addTail(value: T): DoublyLinkedListNode<T> {
if (!this.tail) {
return this.addHead(value);
}
const node = new DoublyLinkedListNode(value);
this.tail = this.addAfter(this.tail, node);
return this.tail;
}
remove(value: T): void {
const node: DoublyLinkedListNode<T> | undefined = super.find(value);
this.removeNode(node);
}
removeNode(node?: DoublyLinkedListNode<T>): void {
if (!node) {
return;
}
if (!node.previous) {
this.head = node.next;
} else {
node.previous.next = node.next;
}
if (!node.next) {
this.tail = node.previous;
} else {
node.next.previous = node.previous;
}
this.length--;
}
removeHead() {
this.removeNode(this.head);
}
removeTail() {
this.removeNode(this.tail);
}
}
// const doublyLinkedList = new DoublyLinkedList<number>();
// console.log(doublyLinkedList);
// doublyLinkedList.addHead(1);
// doublyLinkedList.addHead(2);
// doublyLinkedList.addHead(3);
// doublyLinkedList.addTail(1);
// doublyLinkedList.addTail(2);
// doublyLinkedList.addTail(3);
// console.log(doublyLinkedList);
// doublyLinkedList.removeTail();
// console.log(doublyLinkedList);