前提       对于大多数的大一,大二的童鞋们来说,可能最操蛋的就是数据结构这个课了,什么链表,堆栈,队列,图,简直噩梦!对我也是,我也是在大三后实习后发现这个真的是个硬技能,
      链表的实现       数据结构第一个就是链表了,链表分为两种有直接的数组形式的顺序链,这里不讨论,什么array_push(),array_pop(),函数基本能满足日常的需求,但报告老板,我就是想装个x
上代码吧   melem=null;        $this->mnext=null;    }}class singlelinkedlist{     //头结点数据    public $melem;    //下一结点指针    public $mnext;    //单链表长度    public static $mlength=0;    public function __construct(){        $this->melem=null;        $this->mnext=null;    }    //返回单链表长度      public static function getlength(){          return self::$mlength;    }      public function getisempty(){          if(self::$mlength==0 && $this->mnext==null){              return true;          }          else{              return false;          }      }      public function clearsll(){             if(self::$mlength>0){                while($this->mnext!=null){                        $q=$this->mnext->mnext;                        $this->mnext=null;                        unset($this->mnext);                        $this->mnext=$q;               }           self::$mlength=0;        }    }    public function getheadcreatesll($sarr){        $this->clearsll();        if(is_array($sarr) and count($sarr)>0){            foreach ($sarr as $key => $value) {                $p= new lnode;                $p->melem=$value;                $p->mnext=$this->mnext;                $this->mnext=$p;                self::$mlength++;            }        }        else{            return false;        }        return true;    }     public function gettailcreatesll($sarr){        $this->clearsll();        if(is_array($sarr) and count($sarr)>0){                $q=$this;                foreach($sarr as $value){                        $p=new lnode;                        $p->melem=$value;                        $p->mnext=$q->mnext;                        $q->mnext=$p;                        $q=$p;                        self::$mlength++;               }        }        else{                return false;        }    }     public function getelemforpos($i){         if(is_numeric($i) && $i0){             $p=$this->mnext;             for ($j=1; $j mnext;                 $p=$q;             }             return $p->melem;         }         else{             return null;         }     }      public function getelemisexist($value){          if($value){              $p=$this;              while($p->mnext!=null and $p->melem!=value){                  $q=$p->mnext;                 $p=$q;              }              if($p->melem==value){                  return true;              }              else{                  return false;              }          }      }      public function getelemposition($value){          if($value){              $p=$this;              $pos=0;              while($p->mnext!=null and $p->melem!=$value){                  $q=$p->mnext;                 $p=$q;                 $pos++;              }              if($p->melem==$value){                  return $pos;              }              else{                  return -1;              }          }      }           /*单链表的插入操作     *     *@param int $i 插入元素的位序,即在什么位置插入新的元素,从1开始     *@param mixed $e 插入的新的元素值     *@return boolean 插入成功返回true,失败返回false     */           public function getinsertelem($i,$e){               if($imnext!=null and $jmnext;                 $p=$q;                 $j++;               }               $q=new lnode;               $q->melem=$e;               $q->mnext=$p->mnext;               $p->mnext=$q;               self::$mlength++;               return true;           }      /**     *删除单链中第$i个元素     *@param int $i 元素位序     *@return boolean 删除成功返回true,失败返回false     */    public function getdeleteelem($i){        if($i>self::$mlength || $imnext;                $p->mnext=$q->mnext;                unset($q);                self::$mlength--;                return true;            }    }     public function getallelem(){         $all=array();         if(!$this->getisempty()){             $p=$this->mnext;             while($p->mnext){                 $all[]=$p->melem;                 $p=$p->mnext;             }             if($p->melem)                 $all[]=$p->melem;             return $all;         }     }     public function getelemunique(){            if(!$this->getisempty()){                $p=$this;                while($p->mnext!=null){                        $q=$p->mnext;                        $ptr=$p;                        while($q->mnext!=null){                                if(strcmp($p->melem,$q->melem)===0){                                    $ptr->mnext=$q->mnext;                                    $q->mnext=null;                                    unset($q->mnext);                                    $q=$ptr->mnext;                                    self::$mlength--;                                }                                else{                                    $ptr=$q;                                    $q=$q->mnext;                                }                      }                      //处理最后一个元素                   if(strcmp($p->melem,$q->melem)===0){                                $ptr->mnext=null;                                self::$mlength--;                        }                        $p=$p->mnext;                }//end of while            }        }}///////////////test//////////$node=new singlelinkedlist;$arr=array('gbw','michael','php','js');//$node->getheadcreatesll($arr);//print_r($node->getallelem());$node->gettailcreatesll($arr);echo $node->getelemforpos(2);$pos=$node->getelemposition('gbw');echo $pos;$node->getdeleteelem($pos);$node->getinsertelem(1,'gbw2');print_r($node->getallelem());
希望对大家的php程序设计有所帮助!
   
 
   