Guest User

Untitled

a guest
Jul 20th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Title: Single linked list
  5. * Description: Implementation of a single linked list in PHP
  6. * @author Sameer Borate | codediesel.com
  7. * @version 1.0 20th June 2008
  8. */
  9.  
  10. class ListNode
  11. {
  12. /* Data to hold */
  13. public $data;
  14. public $hash;
  15.  
  16. /* Link to next node */
  17. public $next;
  18.  
  19.  
  20. /* Node constructor */
  21. function __construct($data, $hash)
  22. {
  23. $this->data = $data;
  24. $this->hash = $hash;
  25. $this->next = NULL;
  26. }
  27.  
  28. function readNode()
  29. {
  30. return $this->data;
  31. }
  32. }
  33.  
  34.  
  35. class LinkList
  36. {
  37. /* Link to the first node in the list */
  38. private $firstNode;
  39.  
  40. /* Link to the last node in the list */
  41. private $lastNode;
  42.  
  43. /* Total nodes in the list */
  44. private static $count;
  45.  
  46.  
  47.  
  48. /* List constructor */
  49. function __construct()
  50. {
  51. $this->firstNode = NULL;
  52. $this->lastNode = NULL;
  53. self::$count = 0;
  54. }
  55.  
  56. public function isEmpty()
  57. {
  58. return ($this->firstNode == NULL);
  59. }
  60.  
  61. public function insertFirst($data, $hash)
  62. {
  63. $link = new ListNode($data, $hash);
  64. $link->next = $this->firstNode;
  65. $this->firstNode = &$link;
  66.  
  67. /* If this is the first node inserted in the list
  68. then set the lastNode pointer to it.
  69. */
  70. if($this->lastNode == NULL)
  71. $this->lastNode = &$link;
  72.  
  73. self::$count++;
  74. }
  75.  
  76. public function insertLast($data)
  77. {
  78. if($this->firstNode != NULL)
  79. {
  80. $link = new ListNode($data);
  81. $this->lastNode->next = $link;
  82. $link->next = NULL;
  83. $this->lastNode = &$link;
  84. self::$count++;
  85. }
  86. else
  87. {
  88. $this->insertFirst($data);
  89. }
  90. }
  91.  
  92. public function deleteFirstNode()
  93. {
  94. $temp = $this->firstNode;
  95. $this->firstNode = $this->firstNode->next;
  96. if($this->firstNode != NULL)
  97. self::$count--;
  98.  
  99. return $temp;
  100. }
  101.  
  102. public function deleteLastNode()
  103. {
  104. if($this->firstNode != NULL)
  105. {
  106. if($this->firstNode->next == NULL)
  107. {
  108. $this->firstNode = NULL;
  109. self::$count--;
  110. }
  111. else
  112. {
  113. $previousNode = $this->firstNode;
  114. $currentNode = $this->firstNode->next;
  115.  
  116. while($currentNode->next != NULL)
  117. {
  118. $previousNode = $currentNode;
  119. $currentNode = $currentNode->next;
  120. }
  121.  
  122. $previousNode->next = NULL;
  123. self::$count--;
  124. }
  125. }
  126. }
  127.  
  128. public function deleteNode($key)
  129. {
  130. $current = $this->firstNode;
  131. $previous = $this->firstNode;
  132.  
  133. while($current->data != $key)
  134. {
  135. if($current->next == NULL)
  136. return NULL;
  137. else
  138. {
  139. $previous = $current;
  140. $current = $current->next;
  141. }
  142. }
  143.  
  144. if($current == $this->firstNode)
  145. $this->firstNode = $this->firstNode->next;
  146. else
  147. $previous->next = $current->next;
  148.  
  149. self::$count--;
  150. }
  151.  
  152. public function find($key)
  153. {
  154. $current = $this->firstNode;
  155. while($current->hash != $key)
  156. {
  157. if($current->next == NULL)
  158. return null;
  159. else
  160. $current = $current->next;
  161. }
  162. return $current->data;
  163. }
  164.  
  165. public function readNode($nodePos)
  166. {
  167. if($nodePos <= self::$count)
  168. {
  169. $current = $this->firstNode;
  170. $pos = 1;
  171. while($pos != $nodePos)
  172. {
  173. if($current->next == NULL)
  174. return null;
  175. else
  176. $current = $current->next;
  177.  
  178. $pos++;
  179. }
  180. return $current->data;
  181. }
  182. else
  183. return NULL;
  184. }
  185.  
  186. public function totalNodes()
  187. {
  188. return self::$count;
  189. }
  190.  
  191. public function readList()
  192. {
  193. $listData = array();
  194. $current = $this->firstNode;
  195.  
  196. while($current != NULL)
  197. {
  198. array_push($listData, $current->readNode());
  199. $current = $current->next;
  200. }
  201. return $listData;
  202. }
  203.  
  204. public function reverseList()
  205. {
  206. if($this->firstNode != NULL)
  207. {
  208. if($this->firstNode->next != NULL)
  209. {
  210. $current = $this->firstNode;
  211. $new = NULL;
  212.  
  213. while ($current != NULL)
  214. {
  215. $temp = $current->next;
  216. $current->next = $new;
  217. $new = $current;
  218. $current = $temp;
  219. }
  220. $this->firstNode = $new;
  221. }
  222. }
  223. }
  224. }
  225.  
  226. ?>
Add Comment
Please, Sign In to add comment