Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Title: Single linked list
- * Description: Implementation of a single linked list in PHP
- * @author Sameer Borate | codediesel.com
- * @version 1.0 20th June 2008
- */
- class ListNode
- {
- /* Data to hold */
- public $data;
- public $hash;
- /* Link to next node */
- public $next;
- /* Node constructor */
- function __construct($data, $hash)
- {
- $this->data = $data;
- $this->hash = $hash;
- $this->next = NULL;
- }
- function readNode()
- {
- return $this->data;
- }
- }
- class LinkList
- {
- /* Link to the first node in the list */
- private $firstNode;
- /* Link to the last node in the list */
- private $lastNode;
- /* Total nodes in the list */
- private static $count;
- /* List constructor */
- function __construct()
- {
- $this->firstNode = NULL;
- $this->lastNode = NULL;
- self::$count = 0;
- }
- public function isEmpty()
- {
- return ($this->firstNode == NULL);
- }
- public function insertFirst($data, $hash)
- {
- $link = new ListNode($data, $hash);
- $link->next = $this->firstNode;
- $this->firstNode = &$link;
- /* If this is the first node inserted in the list
- then set the lastNode pointer to it.
- */
- if($this->lastNode == NULL)
- $this->lastNode = &$link;
- self::$count++;
- }
- public function insertLast($data)
- {
- if($this->firstNode != NULL)
- {
- $link = new ListNode($data);
- $this->lastNode->next = $link;
- $link->next = NULL;
- $this->lastNode = &$link;
- self::$count++;
- }
- else
- {
- $this->insertFirst($data);
- }
- }
- public function deleteFirstNode()
- {
- $temp = $this->firstNode;
- $this->firstNode = $this->firstNode->next;
- if($this->firstNode != NULL)
- self::$count--;
- return $temp;
- }
- public function deleteLastNode()
- {
- if($this->firstNode != NULL)
- {
- if($this->firstNode->next == NULL)
- {
- $this->firstNode = NULL;
- self::$count--;
- }
- else
- {
- $previousNode = $this->firstNode;
- $currentNode = $this->firstNode->next;
- while($currentNode->next != NULL)
- {
- $previousNode = $currentNode;
- $currentNode = $currentNode->next;
- }
- $previousNode->next = NULL;
- self::$count--;
- }
- }
- }
- public function deleteNode($key)
- {
- $current = $this->firstNode;
- $previous = $this->firstNode;
- while($current->data != $key)
- {
- if($current->next == NULL)
- return NULL;
- else
- {
- $previous = $current;
- $current = $current->next;
- }
- }
- if($current == $this->firstNode)
- $this->firstNode = $this->firstNode->next;
- else
- $previous->next = $current->next;
- self::$count--;
- }
- public function find($key)
- {
- $current = $this->firstNode;
- while($current->hash != $key)
- {
- if($current->next == NULL)
- return null;
- else
- $current = $current->next;
- }
- return $current->data;
- }
- public function readNode($nodePos)
- {
- if($nodePos <= self::$count)
- {
- $current = $this->firstNode;
- $pos = 1;
- while($pos != $nodePos)
- {
- if($current->next == NULL)
- return null;
- else
- $current = $current->next;
- $pos++;
- }
- return $current->data;
- }
- else
- return NULL;
- }
- public function totalNodes()
- {
- return self::$count;
- }
- public function readList()
- {
- $listData = array();
- $current = $this->firstNode;
- while($current != NULL)
- {
- array_push($listData, $current->readNode());
- $current = $current->next;
- }
- return $listData;
- }
- public function reverseList()
- {
- if($this->firstNode != NULL)
- {
- if($this->firstNode->next != NULL)
- {
- $current = $this->firstNode;
- $new = NULL;
- while ($current != NULL)
- {
- $temp = $current->next;
- $current->next = $new;
- $new = $current;
- $current = $temp;
- }
- $this->firstNode = $new;
- }
- }
- }
- }
- ?>
Add Comment
Please, Sign In to add comment