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