-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoublyLinkedList.ts
129 lines (108 loc) · 3.33 KB
/
DoublyLinkedList.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
export class LinkNode<T>{
constructor(public data: T, public next?: LinkNode<T>, public prev?: LinkNode<T>) { }
}
export class DoublyLinkedList<T>{
constructor(private head?: LinkNode<T>, private tail?: LinkNode<T>, private size: number = 0) {
if (head) {
this.tail = this.head;
this.size++;
}
}
public getSize = (): number => this.size;
public isEmpty = () : boolean => this.size === 0;
public peekFront = () => this.head;
public peekBack = () => this.tail;
public addFront = (node: LinkNode<T>) => {
if (!this.head) {
this.head = this.tail = node;
this.size++
} else {
node.next = this.head;
this.head.prev = node;
this.head = node;
this.size++;
}
}
public addBack = (node: LinkNode<T>) => {
if (!this.tail) {
this.head = this.tail = node;
this.size++
} else {
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
this.size++;
}
}
public deleteNode = (index: number) => {
let start = this.head;
let counter = 0;
let searched = null;
while(start){
if(counter > index)
return;
if(counter === index){
searched = start;
break;
}
start=start.next;
counter++;
}
if(searched === this.head){
this.head = this.head.next;
this.size--;
return;
}
if(searched === this.tail){
this.tail.prev ? this.tail.prev.next = undefined : null;
this.tail = this.tail.prev;
this.size--;
return;
}
if(searched !== null && searched.prev){
searched.prev.next = searched.next;
if(searched.next){
searched.next.prev = searched.prev;
}
searched = undefined;
this.size--;
return;
}
}
public search = (comparator: (searched: T) => boolean): LinkNode<T> | null => {
let start = this.head;
while (start) {
if (comparator(start.data))
return start;
start = start.next;
}
return null;
}
public traverse = (): T[] => {
let start = this.head;
let arr = [];
while (start) {
arr.push(start.data);
start = start.next;
}
return arr;
}
}
/*****************************************************************************************************************/
interface Book {
name: string;
}
const node1 = new LinkNode<Book>({ name: 'Harry Potter' })
const node2 = new LinkNode<Book>({ name: 'Lord of the Rings' })
const node3 = new LinkNode<Book>({ name: 'World Economics' })
const node4 = new LinkNode<Book>({ name: 'Algorithms and Data Structures' })
const linkedList = new DoublyLinkedList<Book>();
linkedList.addFront(node1)
linkedList.addBack(node2);
linkedList.addBack(node3);
linkedList.addFront(node4);
linkedList.deleteNode(2);
//console.log(linkedList.getSize());
console.log(linkedList.search(({name})=> name === 'Harry Potter'));
// console.log(linkedList.peekFront());
// console.log(linkedList.traverse());