PHP数据结构练习笔记--栈

 1 <?php
 2     //栈类
 3 class MyStack{
 4     //变量:数组
 5     private $arr;
 6     
 7     //构造函数
 8     function __construct()
 9     {
10         $this->arr=array();
11         echo "初始化:";
12     }
13     
14     //析构函数
15     function __destruct()
16     {
17         unset($this->arr);
18         echo "释放资源";
19     }
20     
21     //获取栈顶元素
22     function GetTop()
23     {
24         if(count($this->arr)!=0)
25         {
26             return $this->arr[count($this->arr)-1];
27         }
28         else{
29             return false;
30         }
31     }
32     
33     //入栈操作
34     function Push($data)
35     {
36         array_push($this->arr,$data);
37     }
38     
39     //出栈操作
40     function Pop()
41     {
42         if(count($this->arr)!=0)
43         {    
44             array_pop($this->arr);
45         }
46         else{
47             return false;
48         }
49     }
50     
51     //遍历整个栈
52     function StackTraverse()
53     {
54         print_r($this->arr);
55     }
56     
57     //清空栈
58     function ClearStack()
59     {
60         unset($this->arr);
61         $this->arr=array();
62     }
63     
64     //判断栈是否为空
65     function StackEmpty()
66     {
67         if(count($this->arr)==0)
68         {
69             return true;
70         }
71         else{
72             return false;
73         }
74     }
75     
76     //获取栈长度
77     function StackLength()
78     {
79         return count($this->arr);
80     }
81 }
82     
83 ?>

自己做练习写的栈类,没有遇到太多问题,实现起来和线性表大同小异。

posted on 2013-10-04 14:10  macctown  阅读(252)  评论(0编辑  收藏  举报

导航