用php模拟一个简单的堆栈类
<?php
class stack{        
    private 
$top;        
    private 
$base;        
    private 
$myArr = array();        
    function 
__construct(){        
        
$this->top = -1;        
        
$this->base = -1;        
    }        
    function 
push($e){        
        
$this->top++;        
        
$this->myArr[$this->top] = $e;          
    }        
    function 
pop(){        
        if(
$this->top == -1){        
            echo 
'已经到了栈底!';        
        }else{        
            
$this->top--;        
        }        
    }        
    function 
echoStack(){        
        for(
$i 0$i<=$this->top$i++){        
            echo 
$this->myArr[$i] ;        
        }        
    }        
            
}        
$myStack = new stack();        
$myStack->push(100);        
$myStack->push(200);        
$myStack->push(300);        
$myStack->push(400);        
$myStack->echoStack();        
$myStack->pop();        
$myStack->echoStack();        
?>     
posted on 2010-03-21 08:36  fancing  阅读(270)  评论(0编辑  收藏  举报