您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP单链表的基本操作实例分享

2025/3/15 5:59:23发布14次查看
数据结构第一个就是链表了,链表分为两种有直接的数组形式的顺序链,这里不讨论,什么array_push(),array_pop(),函数基本能满足日常的需求,本文主要和大家分享php单链表的基本操作实例,希望能帮助到大家。
<?php /** *@author:gongbangwei(18829212319@163.com) *@version:1.0 *@date:2016-05-22 *单链表的基本操作 *1.初始化单链表 __construct() *2.清空单链表 clearsll() *3.返回单链表长度 getlength() *4.判断单链表是否为空 getisempty() *5.头插入法建表 getheadcreatesll() *6.尾插入法建表 gettailcreatesll() *7.返回第$i个元素 getelemforpos() *8.查找单链表中是否存在某个值的元素 getelemisexist() *9.单链表的插入操作 getinsertelem() *10.遍历单链表中的所有元素 getallelem() *11.删除单链中第$i个元素 getdeleteelem() *12.删除单链表所有重复的值 getelemunique() **/ header("content-type:text/html;charset=utf-8"); class lnode{ public $melem; public $mnext; public function __construct(){ $this->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) && $i<self::$mlength && $i>0){ $p=$this->mnext; for ($j=1; $j < $i ; $j++) { $q=$p->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($i<self::$mlength){ $j=1; $p=$this; } else{ return false; } while($p->mnext!=null and $j<$i){ $q=$p->mnext; $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 || $i<1){ return false; } else{ $p=$this; $j=1; while($j<$i){ $p=$p->mnext; $j++; } $q=$p->mnext; $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单链表翻转
php单链表实现_php教程
php单链表实现
以上就是php单链表的基本操作实例分享的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product