1+ <?php 
2+ 
3+ /*  
4+  * To change this license header, choose License Headers in Project Properties. 
5+  * To change this template file, choose Tools | Templates 
6+  * and open the template in the editor. 
7+  */ 
8+ namespace  DS \LinkedList \Classes ;
9+ use  \DS \LinkedList \Classes \ListNode ;
10+ 
11+ class  DoublyLinkedList {
12+ 
13+     private  $ _firstNode  = NULL ;
14+     private  $ _lastNode  = NULL ;
15+     private  $ _totalNode  = 0 ;
16+ 
17+     public  function  insertAtFirst (string  $ data  = NULL ) {
18+         $ newNode  = new  ListNode ($ data );
19+         if  ($ this  ->_firstNode  === NULL ) {
20+             $ this  ->_firstNode  = &$ newNode ;
21+             $ this  ->_lastNode  = $ newNode ;
22+         } else  {
23+             $ currentFirstNode  = $ this  ->_firstNode ;
24+             $ this  ->_firstNode  = &$ newNode ;
25+             $ newNode ->next  = $ currentFirstNode ;
26+             $ currentFirstNode ->prev  = $ newNode ;
27+         }
28+         $ this  ->_totalNode ++;
29+         return  TRUE ;
30+     }
31+ 
32+     public  function  insertAtLast (string  $ data  = NULL ) {
33+         $ newNode  = new  ListNode ($ data );
34+         if  ($ this  ->_firstNode  === NULL ) {
35+             $ this  ->_firstNode  = &$ newNode ;
36+             $ this  ->_lastNode  = $ newNode ;
37+         } else  {
38+             $ currentNode  = $ this  ->_lastNode ;
39+             $ currentNode ->next  = $ newNode ;
40+             $ newNode ->prev  = $ currentNode ;
41+             $ this  ->_lastNode  = $ newNode ;
42+         }
43+         $ this  ->_totalNode ++;
44+         return  TRUE ;
45+     }
46+ 
47+     public  function  insertBefore (string  $ data  = NULL , string  $ query  = NULL ) {
48+         $ newNode  = new  ListNode ($ data );
49+ 
50+         if  ($ this  ->_firstNode ) {
51+             $ previous  = NULL ;
52+             $ currentNode  = $ this  ->_firstNode ;
53+             while  ($ currentNode  !== NULL ) {
54+                 if  ($ currentNode ->data  === $ query ) {
55+                     $ newNode ->next  = $ currentNode ;
56+                     $ currentNode ->prev  = $ newNode ;
57+                     $ previous ->next  = $ newNode ;
58+                     $ newNode ->prev  = $ previous ;
59+                     $ this  ->_totalNode ++;
60+                     break ;
61+                 }
62+                 $ previous  = $ currentNode ;
63+                 $ currentNode  = $ currentNode ->next ;
64+             }
65+         }
66+     }
67+ 
68+     public  function  insertAfter (string  $ data  = NULL , string  $ query  = NULL ) {
69+         $ newNode  = new  ListNode ($ data );
70+ 
71+         if  ($ this  ->_firstNode ) {
72+             $ nextNode  = NULL ;
73+             $ currentNode  = $ this  ->_firstNode ;
74+             while  ($ currentNode  !== NULL ) {
75+                 if  ($ currentNode ->data  === $ query ) {
76+                     if  ($ nextNode  !== NULL ) {
77+                         $ newNode ->next  = $ nextNode ;
78+                     }
79+                     if  ($ currentNode  === $ this  ->_lastNode ) {
80+                         $ this  ->_lastNode  = $ newNode ;
81+                     }
82+                     $ currentNode ->next  = $ newNode ;
83+                     $ nextNode ->prev  = $ newNode ;
84+                     $ newNode ->prev  = $ currentNode ;
85+                     $ this  ->_totalNode ++;
86+                     break ;
87+                 }
88+                 $ currentNode  = $ currentNode ->next ;
89+                 $ nextNode  = $ currentNode ->next ;
90+             }
91+         }
92+     }
93+ 
94+     public  function  deleteFirst () {
95+         if  ($ this  ->_firstNode  !== NULL ) {
96+             if  ($ this  ->_firstNode ->next  !== NULL ) {
97+                 $ this  ->_firstNode  = $ this  ->_firstNode ->next ;
98+                 $ this  ->_firstNode ->prev  = NULL ;
99+             } else  {
100+                 $ this  ->_firstNode  = NULL ;
101+             }
102+             $ this  ->_totalNode --;
103+             return  TRUE ;
104+         }
105+         return  FALSE ;
106+     }
107+ 
108+     public  function  deleteLast () {
109+         if  ($ this  ->_lastNode  !== NULL ) {
110+ 
111+             $ currentNode  = $ this  ->_lastNode ;
112+             if  ($ currentNode ->prev  === NULL ) {
113+                 $ this  ->_firstNode  = NULL ;
114+                 $ this  ->_lastNode  = NULL ;
115+             } else  {
116+                 $ previousNode  = $ currentNode ->prev ;
117+                 $ this  ->_lastNode  = $ previousNode ;
118+                 $ previousNode ->next  = NULL ;
119+                 $ this  ->_totalNode --;
120+                 return  TRUE ;
121+             }
122+         }
123+         return  FALSE ;
124+     }
125+ 
126+     public  function  delete (string  $ query  = NULL ) {
127+         if  ($ this  ->_firstNode ) {
128+             $ previous  = NULL ;
129+             $ currentNode  = $ this  ->_firstNode ;
130+             while  ($ currentNode  !== NULL ) {
131+                 if  ($ currentNode ->data  === $ query ) {
132+                     if  ($ currentNode ->next  === NULL ) {
133+                         $ previous ->next  = NULL ;
134+                     } else  {
135+                         $ previous ->next  = $ currentNode ->next ;
136+                         $ currentNode ->next ->prev  = $ previous ;
137+                     }
138+ 
139+                     $ this  ->_totalNode --;
140+                     break ;
141+                 }
142+                 $ previous  = $ currentNode ;
143+                 $ currentNode  = $ currentNode ->next ;
144+             }
145+         }
146+     }
147+ 
148+     public  function  displayForward () {
149+         echo  "Total book titles:  "  . $ this  ->_totalNode  . "\n" ;
150+         $ currentNode  = $ this  ->_firstNode ;
151+         while  ($ currentNode  !== NULL ) {
152+             echo  $ currentNode ->data  . "\n" ;
153+             $ currentNode  = $ currentNode ->next ;
154+         }
155+     }
156+ 
157+     public  function  displayBackward () {
158+         echo  "Total book titles:  "  . $ this  ->_totalNode  . "\n" ;
159+         $ currentNode  = $ this  ->_lastNode ;
160+         while  ($ currentNode  !== NULL ) {
161+             echo  $ currentNode ->data  . "\n" ;
162+             $ currentNode  = $ currentNode ->prev ;
163+         }
164+     }
165+ 
166+     public  function  getSize () {
167+         return  $ this  ->_totalNode ;
168+     }
169+ 
170+ }
0 commit comments