1
1
let LinkedList2 = ( function ( ) {
2
2
3
3
class Node {
4
- constructor ( element , priority ) {
4
+ constructor ( element ) {
5
5
this . element = element ;
6
6
this . next = null ;
7
7
}
8
8
}
9
9
10
- let length = 0 ;
11
- let head = null ;
10
+ const length = new WeakMap ( ) ;
11
+ const head = new WeakMap ( ) ;
12
12
13
13
class LinkedList2 {
14
14
15
+ constructor ( ) {
16
+ length . set ( this , 0 ) ;
17
+ head . set ( this , null ) ;
18
+ }
19
+
15
20
append ( element ) {
16
21
17
22
let node = new Node ( element ) ,
18
- current ;
23
+ current ,
24
+ _head = this . getHead ( ) ;
19
25
20
- if ( head === null ) { //first node on list
21
- head = node ;
26
+ if ( _head === null ) { //first node on list
27
+ _head = node ;
28
+ head . set ( this , _head ) ;
22
29
} else {
23
30
24
- current = head ;
31
+ current = _head ;
25
32
26
33
//loop the list until find last item
27
34
while ( current . next ) {
@@ -32,23 +39,28 @@ let LinkedList2 = (function () {
32
39
current . next = node ;
33
40
}
34
41
35
- length ++ ; //update size of list
42
+ //update size of list
43
+ let l = this . size ( ) ;
44
+ l ++ ;
45
+ length . set ( this , l ) ;
36
46
}
37
47
38
48
insert ( position , element ) {
39
49
40
50
//check for out-of-bounds values
41
- if ( position >= 0 && position <= length ) {
51
+ if ( position >= 0 && position <= this . size ( ) ) {
42
52
43
53
let node = new Node ( element ) ,
44
- current = head ,
54
+ _head = this . getHead ( ) ,
55
+ current = _head ,
45
56
previous ,
46
57
index = 0 ;
47
58
48
59
if ( position === 0 ) { //add on first position
49
60
50
61
node . next = current ;
51
- head = node ;
62
+ _head = node ;
63
+ head . set ( this , _head ) ;
52
64
53
65
} else {
54
66
while ( index ++ < position ) {
@@ -59,7 +71,10 @@ let LinkedList2 = (function () {
59
71
previous . next = node ;
60
72
}
61
73
62
- length ++ ; //update size of list
74
+ //update size of list
75
+ let l = this . size ( ) ;
76
+ l ++ ;
77
+ length . set ( this , l ) ;
63
78
64
79
return true ;
65
80
@@ -71,15 +86,17 @@ let LinkedList2 = (function () {
71
86
removeAt ( position ) {
72
87
73
88
//check for out-of-bounds values
74
- if ( position > - 1 && position < length ) {
89
+ if ( position > - 1 && position < this . size ( ) ) {
75
90
76
- let current = head ,
91
+ let _head = this . getHead ( ) ,
92
+ current = _head ,
77
93
previous ,
78
94
index = 0 ;
79
95
80
96
//removing first item
81
97
if ( position === 0 ) {
82
- head = current . next ;
98
+ _head = current . next ;
99
+ head . set ( this , _head ) ;
83
100
} else {
84
101
85
102
while ( index ++ < position ) {
@@ -92,7 +109,9 @@ let LinkedList2 = (function () {
92
109
previous . next = current . next ;
93
110
}
94
111
95
- length -- ;
112
+ let l = this . size ( ) ;
113
+ l -- ;
114
+ length . set ( this , l ) ;
96
115
97
116
return current . element ;
98
117
@@ -109,7 +128,8 @@ let LinkedList2 = (function () {
109
128
110
129
indexOf ( element ) {
111
130
112
- let current = head ,
131
+ let _head = this . getHead ( ) ,
132
+ current = _head ,
113
133
index = 0 ;
114
134
115
135
while ( current ) {
@@ -124,24 +144,24 @@ let LinkedList2 = (function () {
124
144
}
125
145
126
146
isEmpty ( ) {
127
- return length === 0 ;
147
+ return this . size ( ) === 0 ;
128
148
}
129
149
130
150
size ( ) {
131
- return length ;
151
+ return length . get ( this ) ;
132
152
}
133
153
134
154
getHead ( ) {
135
- return head ;
155
+ return head . get ( this ) ;
136
156
}
137
157
138
158
toString ( ) {
139
159
140
- let current = head ,
160
+ let current = this . getHead ( ) ,
141
161
string = '' ;
142
162
143
163
while ( current ) {
144
- string += current . element + ( current . next ? '\n ' : '' ) ;
164
+ string += current . element + ( current . next ? ', ' : '' ) ;
145
165
current = current . next ;
146
166
}
147
167
return string ;
0 commit comments