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