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  $ _firstNodeNULL ;
14+     private  $ _lastNodeNULL ;
15+     private  $ _totalNode0 ;
16+ 
17+     public  function  insertAtFirst (string  $ dataNULL ) {
18+         $ newNodenew  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+             $ newNodenext  = $ currentFirstNode
26+             $ currentFirstNodeprev  = $ newNode
27+         }
28+         $ this _totalNode ++;
29+         return  TRUE ;
30+     }
31+ 
32+     public  function  insertAtLast (string  $ dataNULL ) {
33+         $ newNodenew  ListNode ($ data
34+         if  ($ this _firstNode  === NULL ) {
35+             $ this _firstNode  = &$ newNode
36+             $ this _lastNode  = $ newNode
37+         } else  {
38+             $ currentNode$ this _lastNode ;
39+             $ currentNodenext  = $ newNode
40+             $ newNodeprev  = $ currentNode
41+             $ this _lastNode  = $ newNode
42+         }
43+         $ this _totalNode ++;
44+         return  TRUE ;
45+     }
46+ 
47+     public  function  insertBefore (string  $ dataNULL , string  $ queryNULL ) {
48+         $ newNodenew  ListNode ($ data
49+ 
50+         if  ($ this _firstNode ) {
51+             $ previousNULL ;
52+             $ currentNode$ this _firstNode ;
53+             while  ($ currentNodeNULL ) {
54+                 if  ($ currentNodedata  === $ query
55+                     $ newNodenext  = $ currentNode
56+                     $ currentNodeprev  = $ newNode
57+                     $ previousnext  = $ newNode
58+                     $ newNodeprev  = $ previous
59+                     $ this _totalNode ++;
60+                     break ;
61+                 }
62+                 $ previous$ currentNode
63+                 $ currentNode$ currentNodenext ;
64+             }
65+         }
66+     }
67+ 
68+     public  function  insertAfter (string  $ dataNULL , string  $ queryNULL ) {
69+         $ newNodenew  ListNode ($ data
70+ 
71+         if  ($ this _firstNode ) {
72+             $ nextNodeNULL ;
73+             $ currentNode$ this _firstNode ;
74+             while  ($ currentNodeNULL ) {
75+                 if  ($ currentNodedata  === $ query
76+                     if  ($ nextNodeNULL ) {
77+                         $ newNodenext  = $ nextNode
78+                     }
79+                     if  ($ currentNode$ this _lastNode ) {
80+                         $ this _lastNode  = $ newNode
81+                     }
82+                     $ currentNodenext  = $ newNode
83+                     $ nextNodeprev  = $ newNode
84+                     $ newNodeprev  = $ currentNode
85+                     $ this _totalNode ++;
86+                     break ;
87+                 }
88+                 $ currentNode$ currentNodenext ;
89+                 $ nextNode$ currentNodenext ;
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  ($ currentNodeprev  === NULL ) {
113+                 $ this _firstNode  = NULL ;
114+                 $ this _lastNode  = NULL ;
115+             } else  {
116+                 $ previousNode$ currentNodeprev ;
117+                 $ this _lastNode  = $ previousNode
118+                 $ previousNodenext  = NULL ;
119+                 $ this _totalNode --;
120+                 return  TRUE ;
121+             }
122+         }
123+         return  FALSE ;
124+     }
125+ 
126+     public  function  delete (string  $ queryNULL ) {
127+         if  ($ this _firstNode ) {
128+             $ previousNULL ;
129+             $ currentNode$ this _firstNode ;
130+             while  ($ currentNodeNULL ) {
131+                 if  ($ currentNodedata  === $ query
132+                     if  ($ currentNodenext  === NULL ) {
133+                         $ previousnext  = NULL ;
134+                     } else  {
135+                         $ previousnext  = $ currentNodenext ;
136+                         $ currentNodenext ->prev  = $ previous
137+                     }
138+ 
139+                     $ this _totalNode --;
140+                     break ;
141+                 }
142+                 $ previous$ currentNode
143+                 $ currentNode$ currentNodenext ;
144+             }
145+         }
146+     }
147+ 
148+     public  function  displayForward () {
149+         echo  "Total book titles:  "  . $ this _totalNode  . "\n" ;
150+         $ currentNode$ this _firstNode ;
151+         while  ($ currentNodeNULL ) {
152+             echo  $ currentNodedata  . "\n" ;
153+             $ currentNode$ currentNodenext ;
154+         }
155+     }
156+ 
157+     public  function  displayBackward () {
158+         echo  "Total book titles:  "  . $ this _totalNode  . "\n" ;
159+         $ currentNode$ this _lastNode ;
160+         while  ($ currentNodeNULL ) {
161+             echo  $ currentNodedata  . "\n" ;
162+             $ currentNode$ currentNodeprev ;
163+         }
164+     }
165+ 
166+     public  function  getSize () {
167+         return  $ this _totalNode ;
168+     }
169+ 
170+ }
0 commit comments