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

储存字符串的PHP单链表

2024/4/11 22:30:13发布16次查看
跳至 [1] [全屏预览] data = $data; $this->next = $next; } }class linkedlist{ private $head; //单链表的头结点,不存储数据 function __construct(){//单链表的构造方法 //头结点的数据为傀儡, 不代表 任何数据 $this->head = new node(dummy 傀儡); $this->first = null; } function isempty(){ return ($this->head->next == null); } /* orderinsert($data) 方法, * 按给定字符串 $data 的大小, 将其安插到适当的位置, * 以保证单链表中字符串的存储,始终是有序的。 */ function orderinsert($data){ $p = new node($data); if($this->isempty()){ $this->head->next = $p; } else{ $q = $this->head; while($q->next != null && strcmp($data, $q->next->data)> 0 ){ $q = $q->next; } $p->next = $q->next; $q->next = $p; } } function insertlast($data){//将字符串插到单链表的尾部 $p = new node($data); if($this->isempty()){ $this->head->next = $p; } else{ $q = $this->head->next; while($q->next != null) $q = $q->next; $q->next = $p; } } function find($value){//查询是否有给定的字符串 $q = $this->head->next; while($q->next != null){ if(strcmp($q->data,$value)==0){ break; } $q = $q->next; } if ($q->data == $value) return $q; else return new node($value. 没有找到!); } function traversal(){//遍历单链表 if(!$this->isempty()){ $p=$this->head->next; echo $p->data. ; while($p->next != null){ $p=$p->next; echo $p->data. ; } }else echo 链表为空!; }} $ll = new linkedlist();$city =array(wuhan, beijing, shanghai, tianjin, changsha, kunming, gongyi,tokyo,new york,ottawa,moskow,edmonton,glasgow,thunder bay,new delhi); for ($i=0;$iorderinsert($city[$i]);$ll->traversal();echo
.$ll->find(gongyi)->data.
;echo $ll->find(baoding)->data.
;?>
2. [图片] output.png
该用户其它信息

VIP推荐

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