1 |
|
2 | export class LinkedList<T> {
|
3 |
|
4 | private first: LinkedListItem<T> = null;
|
5 | private last: LinkedListItem<T> = null;
|
6 |
|
7 | public add(item: T): void {
|
8 | let entry: LinkedListItem<T> = {
|
9 | item: item,
|
10 | next: null
|
11 | };
|
12 | if (this.last) {
|
13 | this.last.next = entry;
|
14 | } else {
|
15 | this.first = entry;
|
16 | }
|
17 | this.last = entry;
|
18 | }
|
19 |
|
20 | public remove(): T {
|
21 | let result = this.first;
|
22 | if (result) {
|
23 | this.first = result.next;
|
24 | if (!this.first) {
|
25 | this.last = null;
|
26 | }
|
27 | }
|
28 | return result.item;
|
29 | }
|
30 |
|
31 | public isEmpty(): boolean {
|
32 | return !this.first;
|
33 | }
|
34 | }
|
35 |
|
36 | class LinkedListItem<T> {
|
37 | item: T;
|
38 | next: LinkedListItem<T>;
|
39 | } |
\ | No newline at end of file |