参考资料:传智播客韩顺平老师一周玩转算法公开课视频

实例:用php单向链表实现水浒英雄排行

  1 <?php
  2     header(\'content-type:text/html;charset=utf-8\');
  3     /**
  4      * 定义水浒英雄排行类
  5      * 可以想像成有一个线性表:Heros = (h1,h2,h3……) $head的位置就是h1前面的那个位置
  6      */
  7     class Heros{
  8         /**
  9          * @var int $id 编号
 10          */
 11         public $id; 
 12         
 13         /**
 14          * @var string $name 真实姓名
 15          */
 16         public $name;
 17         
 18         /**
 19          * @var string $nickname 外号
 20          */
 21         public $nickname;
 22         
 23         /**
 24          * @var object $head hero类的一个实例的下一个对象
 25          */
 26         public $next;
 27         
 28         function __construct($id = \'\', $name = \'\', $nickname = \'\')
 29         {
 30             $this->id = $id;
 31             $this->name = $name;
 32             $this->nickname = $nickname;
 33         }
 34         
 35         
 36         
 37     }
 38 
 39     /**
 40      * 添加水浒英雄
 41      * @param object $head Heros类的空对象(栈顶)
 42      * @param object $hero Heros类的一个实例
 43      */
 44     function addHero($head, $hero)
 45     {
 46         //定义一个当前实例,先初始化
 47         $current = $head;
 48         //定义一个变量代表是否是重复值
 49         $flag = false;
 50         while($current->next != null)
 51         {
 52             if($current->next != null){
 53                 break;
 54             }
 55             else if($current->next->id = $hero->next->id)
 56             {
 57                 $flag = true;
 58             }
 59             $current = $current->next;
 60         }
 61         if(!$flag)
 62         {
 63             $hero->next = $current->next;
 64             $current->next = $hero;
 65         }
 66     
 67     }
 68     
 69     /**
 70      * 修改英雄信息
 71      * @param object $head Heros类的空对象(栈顶)
 72      * @param object $hero Heros类的一个实例
 73      */
 74     function updateHero($head, $hero)
 75     {
 76         $current = $head;
 77         while($current->next != null)
 78         {
 79             if($current->next->id = $hero->id)
 80             {
 81                 break;
 82             }
 83             $current = $current->next;
 84         }
 85         if($current->next == null)
 86         {
 87             echo \'您要修改的编号为\'.$hero->id.\'的英雄不存在!\';
 88         }
 89         else{
 90             $current->next->name = $hero->name;
 91             $current->next->nickname = $hero->nickname;
 92         }
 93     }
 94     
 95     /**
 96      * 删除某个英雄
 97      * @param object $head Heros类的空对象(栈顶)
 98      * @param int $id 英雄编号
 99      */
100     function delHero($head, $id)
101     {
102         $current = $head;
103         while($current->next != null)
104         {
105             if($current->next->id = $id)
106             {
107                 $flag = true;
108                 break;
109             }
110             $current = $current->next;
111         }
112         if(!$flag)
113         {
114             $current->next = $current->next->next;
115         }
116         else{
117             echo \'您要删除的编号为\'.$hero->id.\'的英雄不存在!\';
118         }
119     }
120     
121     /**
122      * 显示英雄排行榜
123      * @param object $head Heros类的空对象(栈顶)
124      */
125     function showHeros($head)
126     {
127         $current = $head;
128         while($current->next != null)
129         {
130             echo \'第\'.$current->next->id.\'位,姓名:\'.$current->next->name.\'外号:\'.$current->next->nickname.\'<br/>\';
131             $current = $current->next;
132         }
133     }
134     
135     $head = new Heros();
136     $h1 = new Heros(1,\'宋江\',\'及时雨\');
137     addHero($head,$h1);
138     $h2 = new Heros(2,\'卢俊义\',\'玉麒麟\');
139     addHero($head,$h2);
140     $h3 = new Heros(3,\'吴用\',\'智多星\');
141     addHero($head,$h3);
142     showHeros($head);
143 
144 ?>

 

一、内存分配(c)

  因为php底层是c

  用上面的例子来说:

    

   关于内存分配的一点说明:

    1、栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中的栈。
     2、堆区(heap) — 一般由程序员分配释放, 若程序员不释放,程序结束时可能由OS回收 。注意它与数据结构中的堆是两回事,分配方式倒是类似于链表,呵呵。
    3、全局区(静态区)(static)—,全局变量和静态变量的存储是放在一块的,初始化的全局变量和静态变量在一块区域, 未初始化的全局变量和未初始化的静态变量在相邻的另一块区域。 – 程序结束后有系统释放
    4、文字常量区 —常量字符串就是放在这里的。 程序结束后由系统释放
    5、程序代码区—存放函数体的二进制代码。

   一般来说:内存分配根据以下规则:

   (1)基本数据类型一般放在栈区

   (2)复合数据类型一般放在堆区

二、案例说明:

  对象实例化的数据在堆区,但其名称在栈区,其结构如下:

    

版权声明:本文为withec原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/withec/archive/2012/11/21/2780958.html