1
+ //Hamza chabchoub contribution for a university project
2
+ function doubleLinkedList ( ) {
3
+ let Node = function ( element ) {
4
+ this . element = element ;
5
+ this . next = null ;
6
+ this . prev = null ;
7
+ }
8
+
9
+ let length = 0 ;
10
+ let head = null ;
11
+ let tail = null ;
12
+
13
+ //Add new element
14
+ this . append = function ( element ) {
15
+ let node = new Node ( element ) ,
16
+ current = head ,
17
+ previous ;
18
+
19
+ if ( ! head ) {
20
+ head = node ;
21
+ tail = node ;
22
+ } else {
23
+ node . prev = tail ;
24
+ tail . next = node ;
25
+ tail = node ;
26
+ }
27
+
28
+ length ++ ;
29
+ }
30
+
31
+
32
+ //Add element
33
+ this . insert = function ( position , element ) {
34
+
35
+ //Check of out-of-bound values
36
+ if ( position >= 0 && position <= length ) {
37
+ let node = new Node ( element ) ,
38
+ current = head ,
39
+ previous ,
40
+ index = 0 ;
41
+
42
+ if ( position === 0 ) {
43
+ if ( ! head ) {
44
+ head = node ;
45
+ tail = node ;
46
+ } else {
47
+ node . next = current ;
48
+ current . prev = node ;
49
+ head = node ;
50
+ }
51
+ } else if ( position === length ) {
52
+ current = tail ;
53
+ current . next = node ;
54
+ node . prev = current ;
55
+ tail = node ;
56
+ } else {
57
+ while ( index ++ < position ) {
58
+ previous = current ;
59
+ current = current . next ;
60
+ }
61
+
62
+ node . next = current ;
63
+ previous . next = node ;
64
+
65
+ //New
66
+ current . prev = node ;
67
+ node . prev = previous ;
68
+ }
69
+
70
+ length ++ ;
71
+ return true ;
72
+ } else {
73
+ return false ;
74
+ }
75
+ }
76
+
77
+ //Remove element at any position
78
+ this . removeAt = function ( position ) {
79
+ //look for out-of-bounds value
80
+ if ( position > - 1 && position < length ) {
81
+ let current = head , previous , index = 0 ;
82
+
83
+ //Removing first item
84
+ if ( position === 0 ) {
85
+ head = current . next ;
86
+
87
+ //if there is only one item, update tail //NEW
88
+ if ( length === 1 ) {
89
+ tail = null ;
90
+ } else {
91
+ head . prev = null ;
92
+ }
93
+ } else if ( position === length - 1 ) {
94
+ current = tail ;
95
+ tail = current . prev ;
96
+ tail . next = null ;
97
+ } else {
98
+ while ( index ++ < position ) {
99
+ previous = current ;
100
+ current = current . next ;
101
+ }
102
+
103
+ //link previous with current's next - skip it
104
+ previous . next = current . next ;
105
+ current . next . prev = previous ;
106
+ }
107
+
108
+ length -- ;
109
+ return current . element ;
110
+ } else {
111
+ return null ;
112
+ }
113
+ }
114
+
115
+ //Get the indexOf item
116
+ this . indexOf = function ( elm ) {
117
+ let current = head ,
118
+ index = - 1 ;
119
+
120
+ //If element found then return its position
121
+ while ( current ) {
122
+ if ( elm === current . element ) {
123
+ return ++ index ;
124
+ }
125
+
126
+ index ++ ;
127
+ current = current . next ;
128
+ }
129
+
130
+ //Else return -1
131
+ return - 1 ;
132
+ } ;
133
+
134
+ //Find the item in the list
135
+ this . isPresent = ( elm ) => {
136
+ return this . indexOf ( elm ) !== - 1 ;
137
+ } ;
138
+
139
+ //Delete an item from the list
140
+ this . delete = ( elm ) => {
141
+ return this . removeAt ( this . indexOf ( elm ) ) ;
142
+ } ;
143
+
144
+ //Delete first item from the list
145
+ this . deleteHead = function ( ) {
146
+ this . removeAt ( 0 ) ;
147
+ }
148
+
149
+ //Delete last item from the list
150
+ this . deleteTail = function ( ) {
151
+ this . removeAt ( length - 1 ) ;
152
+ }
153
+
154
+ //Print item of the string
155
+ this . toString = function ( ) {
156
+ let current = head ,
157
+ string = '' ;
158
+
159
+ while ( current ) {
160
+ string += current . element + ( current . next ? '\n' : '' ) ;
161
+ current = current . next ;
162
+ }
163
+
164
+ return string ;
165
+ } ;
166
+
167
+ //Convert list to array
168
+ this . toArray = function ( ) {
169
+ let arr = [ ] ,
170
+ current = head ;
171
+
172
+ while ( current ) {
173
+ arr . push ( current . element ) ;
174
+ current = current . next ;
175
+ }
176
+
177
+ return arr ;
178
+ } ;
179
+
180
+ //Check if list is empty
181
+ this . isEmpty = function ( ) {
182
+ return length === 0 ;
183
+ } ;
184
+
185
+ //Get the size of the list
186
+ this . size = function ( ) {
187
+ return length ;
188
+ }
189
+
190
+ //Get the head
191
+ this . getHead = function ( ) {
192
+ return head ;
193
+ }
194
+
195
+ //Get the tail
196
+ this . getTail = function ( ) {
197
+ return tail ;
198
+ }
199
+ }
0 commit comments