-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patha-list.ts
More file actions
52 lines (44 loc) · 884 Bytes
/
a-list.ts
File metadata and controls
52 lines (44 loc) · 884 Bytes
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
'use strict';
class ListItem<T> {
public data: T;
public prev: ListItem<T>;
public next: ListItem<T>;
constructor(data: T) {
this.data = data;
this.prev = null;
this.next = null;
}
}
class List<T> {
public head: ListItem<T>;
public tail: ListItem<T>;
constructor() {
this.head = null;
this.tail = null;
}
push(data: T) {
const item = new ListItem<T>(data);
if (this.head === null) {
this.head = item;
} else {
item.prev = this.tail;
this.tail.next = item;
}
this.tail = item;
}
display() {
let current = this.head;
while (current) {
console.log(current.data);
current = current.next;
}
}
}
// Usage
const list = new List<string>();
list.push('Ave');
list.push('Emperor');
//list.push(new List<number>());
list.push('Marcus Aurelius!');
//list.push(1);
list.display();