PHPアルゴリズム毎日練習--スタック
7001 ワード
1 <?php
2 /** */
3 class myStark{
4 public $top = -1;
5 public $maxStark = 5;
6 public $stark = array();
7 public function __construct(){
8
9 }
10 /* */
11 public function push($val){
12 if($this->top > $this->maxStark - 1){
13 return false;
14 }
15 $this->top ++;
16 $this->stark[$this->top] = $val;
17 }
18 /* */
19 public function pop(){
20 if($this->top <= -1){
21 return false;
22 }
23 $return = $this->stark[$this->top];
24 $this->top -- ;
25 return $return;
26 }
27 /* */
28 public function show(){
29 for($i = $this->top;$i>-1;$i--){
30 echo 'stack['.$i.']='.$this->stark[$i].'<br />';
31 }
32 }
33 }
34 $stack = new myStark();
35 $stack->push(' ');
36 $stack->push(' ');
37 $stack->push(' ');
38 $stack->push(' ');
39 $stack->push(' ');
40 $stack->show();
41 $val = $stack->pop();
42 echo $val.' !'.
43 $stack->show();
44 ?>
ソースのダウンロード:Stark_1203.zip