Simple linked list in PHP

This code demonstrate the implementation of linked list using PHP. 
<?  class ListItem{
 var $key;
 var $next;
 var $prev;
   function ListItem($key=NULL){
  $this->key=$key;
  $this->next=NULL;
  $this->prev=NULL;
 }
}
 class LinkedList{
 var $head;
   function LinkedList(){
  $this->head=NULL;
 }
 function ListInsert(&$x){  
  $x->next = &$this->head;
  if(!is_null($this->head))$this->head->prev=&$x;
  $this->head=&$x;
  $this->head->prev=NULL;
 }
  function ListSearch($k){
  $x=&$this->head;
  while(!is_null($x) && $x->key!=$k){
   $x=&$x->next;
  }
 return $x;
 }
  function ListDelete(&$x){
  if (!is_null($x->prev)) $x->prev->next=&$x->next;
  else $this->head=&$x->next;
  if (!is_null($x->next)) $x->next->prev=&$x->prev;
 }
   function Listshow(){
  $x=&$this->head;
  $b=1;
  while ($b){
   echo 'key:'.$x->key."n";
   if(is_null($x->next))$b=0;
   $x=&$x->next;
  } 
  }
 }
?>
Notes: I have difficulties in writing codes in blogsopt. The solution is, write the post in google docs, and send this article to the blogspot. Helluva job Kerul...

Popular Posts

WorldConferences.net