php数据结构课程---2、链表(php中 是如何实现单链表的(也就是php中如何实现对象引用的))

php数据结构课程---2、链表(php中 是如何实现单链表的(也就是php中如何实现对象引用的))

一、总结

一句话总结:

php是弱类型语言,变量即可表示数值,也可表示对象:链表节点的数据域的值就是数值,链表节点的指针域的值就是对象(new Node()出来的产物)
用class来构建节点,也用class来构建比如单链表啊,双链表啊

1、链表是什么?

链表是一种物理存储(内存)单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过对象引用来实现的。
节点=数据域+下一个结点的引用

链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点的引用。

2、php实现单链表实例?

节点是一个类:class Node{}:Node节点里面有两个变量($data和$next)和一个构造函数__construct()
php构造函数:public function __construct(){}
单链表也是一个类:有$header,$last,$size三个变量
  1 <?php 
  2 
  3 
  4 class Node{
  5     public $data = null;
  6     public $next = null;
  7     public function __construct($data,$next=null){
  8         $this->data = $data;
  9         $this->next = $next;
 10     }
 11 }
 12 
 13 class singleLinkList{
 14     private $header = null;
 15     private $last = null;
 16     public $size = 0;
 17 
 18     public function __construct(){
 19         
 20     }
 21 
 22     public function add($data){
 23         $node = new Node($data);
 24         if($this->header == null and $this->last==null){
 25             $this->header = $node;
 26             $this->last = $node;
 27         }else{
 28             $this->last->next = $node;
 29             $this->last = $node;
 30         }
 31     }
 32 
 33     public function del($data){
 34         $node=$this->header;
 35         if($node->data == $data){
 36             $this->header = $this->header->next;
 37             return true;
 38         }else{
 39             while($node->next->data == $data){
 40                 $node->next = $node->next->next;
 41                 return true;
 42             }
 43         }
 44         return false;
 45     }
 46 
 47     public function update($old,$new){
 48         $node = $this->header;
 49         while($node->next != null){
 50             if($node->data == $old){
 51                 $node->data = $new;
 52                 return true;
 53             }
 54             $node = $node->next;
 55         }
 56         echo "not found!";
 57         return false;
 58     }
 59 
 60     public function find($data){
 61         $node = $this->header;
 62         while($node->next != null){
 63             if($node->data == $data){
 64                 echo "found!";
 65                 return;
 66             }
 67             $node = $node->next;
 68         }
 69         echo "not found!";
 70     }
 71 
 72     public function getAll(){
 73         $node = $this->header;
 74         while($node->next != null){
 75             echo $node->data;
 76             $node = $node->next;
 77         }
 78         echo $node->data;
 79     }
 80 }
 81 
 82 $list =  new singleLinkList();
 83 $list->add("1");
 84 $list->add("2");
 85 $list->add("3");
 86 $list->add("4");
 87 $list->add("5");
 88 $list->add("6");
 89 echo "<pre>";
 90 // $list->getAll();
 91 
 92 // if($list->del("2")){
 93 //     echo "success";
 94 // }else{
 95 //     echo "false";
 96 // }
 97 
 98 // if($list->update("3","7")){
 99 //     var_dump($list);
100 // }
101 
102 $list->find(7);
103 // $list->getAll();

3、双向链表的数据结构是怎样?

<-pre data next->:data为数据,pre为指向前个节点的引用,next为指向后个节点的引用
class node{
    public $data;
    public $prev;
    public $next;
}
class doubleLinkList{
    public header;
    private size;
}    //操作函数getSize(),setHeader($v),__construct(),add($d)
//       delete($d),update($d),find($d),

4、循环链表的结构结构是怎样?

和双向链表相似:data域加前($prev)后($next)两个指针:最后一个节点的next指针指向和第一个节点,第一个的prev指向了最后一个节点

5、约瑟夫环(求最后剩下的那个人)如何用循环链表来做,(n个小孩坐在一圈,每数到3就退出去,最后余下的是谁)?

确定好Node节点的属性和方法
确定构造函数:创建链表
circleList的另外一个方法输出最后剩下的那个人
<?php 


class Node{
    public $name;
    public $next;

    public function __construct($name,$next=null){
        $this->name = $name;
        $this->next = $next;
    }

    public function setNext(&$next){
        $this->next = $next;
    }
}

class circleList{
    public $head =null;

    public function __construct($str){
        $node = null;

        foreach (explode(" ", $str) as $key => $value) {
            if($node){
                $node->setNext(new Node($value));
                $node= $node->next;
            }else{
                $node = new Node($value);
                $this->head =  $node;
            }
        }
        $node->setNext($this->head);
        // echo "<pre>";
        // var_dump($this->head);
    }

    public function findLastOne($n){
        $count = 1;
        $list = $this->head;
        while($list->next != $list){
            if($count%3==2){
                $list->next=$list->next->next;
            }else{
                $list=$list->next;
            }
            $count++;
        }

        return $list->name;
    }
}


$str = "圣夏彤 邴丹丹 台浩邈 鲜依瑶 闪又青 威心远 佛乐正 寻仙韵 别光华 柳舒兰 
闻清婉 让昕珏 出欣跃 慎诗丹 卿冬萱 拜若薇 黄梦秋 浮娅玟 骆飞章 剑成化 
平海荣 望凡霜 蔡雪萍 姓慧颖 秋访梦 须盈秀 邰宣朗 林安彤 谬祺然 能骏琛 
汤香菱 查寻琴 乾问凝 冒听筠 左诗兰 市暄美 位水丹 裔翠芙 邗宏博 空永长 
进南霜 司空新雪 穆乐蓉 绪野雪 告天瑞 仁桂帆 齐珠雨 姒以冬 禽夏菡 呼延兰英 
简小雨 建彤蕊 偶迎南 南夏岚 枝傲菡 罗蔚星 年博简 夔代巧 戈曼蔓 赫麦冬 
光美如  戏柔惠 恭千秋 森寄蕾 亓娴淑 豆飞瑶 菅尔容 斋依云 荆紫杉 宦雪容 
酒含景 占碧蓉 牵盼香 唐青雪 清映秋 瑞和璧 蓝听春 牛新洁 沐妙思 胥雍恬 
饶鸿光 丹乐然 抄曼吟 张廖智杰 府念云 昔德宇 尧平安 尹芳泽 勤清妍 哀清莹 
柴佩珍 夹谷昆琦 塔思琪 虞采萱 融水荷 招采萱 势鸿志 次长岳 费莫怀芹 始修真";

$list = new circleList($str);
echo $list->findLastOne(3);

二、内容在总结中

 
原文地址:https://www.cnblogs.com/Renyi-Fan/p/10873352.html