1
- import { defaultCompare , defaultEquals , ICompareFunction , IEqualsFunction } from '../util' ;
1
+ import { defaultEquals , IEqualsFunction } from '../util' ;
2
2
import { Node } from './models/linked-list-models' ;
3
3
4
4
export default class LinkedList < T > {
5
5
protected count = 0 ;
6
6
protected head : Node < T > | undefined ;
7
7
8
- constructor (
9
- protected equalsFn : IEqualsFunction < T > = defaultEquals ,
10
- protected compareFn : ICompareFunction < T > = defaultCompare
11
- ) { }
8
+ constructor ( protected equalsFn : IEqualsFunction < T > = defaultEquals ) { }
12
9
13
10
push ( element : T ) {
14
11
const node = new Node ( element ) ;
15
12
let current ;
16
13
17
- if ( this . head == null ) { // catches null && undefined
14
+ if ( this . head == null ) {
15
+ // catches null && undefined
18
16
this . head = node ;
19
17
} else {
20
18
current = this . head ;
@@ -31,17 +29,15 @@ export default class LinkedList<T> {
31
29
getElementAt ( index : number ) {
32
30
if ( index >= 0 && index <= this . count ) {
33
31
let node = this . head ;
34
- for ( let i = 0 ; i < index ; i ++ ) {
35
- if ( node != null ) {
36
- node = node . next ;
37
- }
32
+ for ( let i = 0 ; i < index && node != null ; i ++ ) {
33
+ node = node . next ;
38
34
}
39
35
return node ;
40
36
}
41
37
return undefined ;
42
38
}
43
39
44
- insert ( index : number , element : T ) {
40
+ insert ( element : T , index : number ) {
45
41
if ( index >= 0 && index <= this . count ) {
46
42
const node = new Node ( element ) ;
47
43
const current = this . head ;
@@ -51,61 +47,28 @@ export default class LinkedList<T> {
51
47
this . head = node ;
52
48
} else {
53
49
const previous = this . getElementAt ( index - 1 ) ;
54
- if ( previous != null ) {
55
- node . next = previous . next ;
56
- previous . next = node ;
57
- }
50
+ node . next = previous . next ;
51
+ previous . next = node ;
58
52
}
59
53
this . count ++ ;
60
54
return true ;
61
55
}
62
56
return false ;
63
57
}
64
58
65
- insertSorted ( element : T ) {
66
- if ( this . isEmpty ( ) ) {
67
- this . push ( element ) ;
68
- } else {
69
- const index = this . getIndexNextSortedElement ( element ) ;
70
- this . insert ( index , element ) ;
71
- }
72
- }
73
-
74
- private getIndexNextSortedElement ( element : T ) {
75
- let current = this . head ;
76
-
77
- for ( let i = 0 ; i < this . size ( ) && current ; i ++ ) {
78
- const comp = this . compareFn ( element , current . element ) ;
79
- if ( comp >= 0 ) {
80
- return i ;
81
- }
82
- current = current . next ;
83
- }
84
-
85
- return - 1 ;
86
- }
87
-
88
59
removeAt ( index : number ) {
89
60
if ( index >= 0 && index < this . count ) {
90
61
let current = this . head ;
91
62
92
63
if ( index === 0 ) {
93
- if ( this . head != null ) {
94
- this . head = this . head . next ;
95
- }
64
+ this . head = current . next ;
96
65
} else {
97
66
const previous = this . getElementAt ( index - 1 ) ;
98
- if ( previous != null ) {
99
- current = previous . next ;
100
- if ( current != null ) {
101
- previous . next = current . next ;
102
- }
103
- }
104
- }
105
- if ( current != null ) {
106
- this . count -- ;
107
- return current . element ;
67
+ current = previous . next ;
68
+ previous . next = current . next ;
108
69
}
70
+ this . count -- ;
71
+ return current . element ;
109
72
}
110
73
return undefined ;
111
74
}
0 commit comments