+ -
当前位置:首页 → 问答吧 → php 队列、堆栈类V1.1

php 队列、堆栈类V1.1

时间:2010-02-01

来源:互联网

/当deQueue()或pop()时,会unset该变量。

//添加destroyStack()

//添加clearQueue()

<?

 //Creator: sheva 
 

 class Queue{
 
  private $queue;
  private $front;
  private $rear;
  
  function Queue(){
   
   $this->queue[0][”content”] = “NULL”;
   $this->front = 0;
   $this->rear = 0;
   
   return true;
  
  }
  
  function enQueue($node){
  
   $this->rear += 1;
   
   $this->queue[$this->rear][”content”] = $node;
   
   return true;
      
  }
  
  function deQueue(){
  
   if($this->front == $this->rear){
   
    return false;
   
   }else{
   
    $this->front += 1;
    
    $temp = $this->queue[$this->front][”content”];
    
    unset($this->queue[$this->front]);
    
    if($this->front == $this->rear){
    
     $this->front = 0;
     $this->rear = 0;
     
    }
    
    return $temp;
    
   }
   
  }
  
  function getHead(){
  
   return $this->queue[$this->front + 1][”content”];
   
  }
  
  function isEmpty(){
  
   if($this->front == $this->rear)
    return true;
   else
    return false;
  
  }
  
  function destroyQueue(){
  
   unset($this->queue);
   unset($this->front);
   unset($this->rear);
   
   return true;
  
  }
  
  function clearQueue(){
  
   for($i = 0; $i <= $this->rear; $i++ ){
    
    $this->queue[$i][”content”] = “”;
   
   }

   $this->front = 0;
   $this->rear = 0;
   
   return true;
  
  }
  
  function queueLength(){
  
   return ($this->rear - $this->front);
   
  }
  
  //获取第$pointer个node, 从1开始
  function getNode($pointer){
  
   $difference = $this->rear - $this->front;
  
   if( $pointer <= $difference ){

    return $this->queue[$pointer][”content”];
  
   }else{
   
    return false;
    
   }
   
  }
  

 }
 
 
?>



<?
 //Creator: sheva 
 

 class Stack{
 
  private $stack;
  private $top;
  private $base;
  
  function Stack(){
   
   $this->stack[0][”content”] = “NULL”;
   $this->top = 0;
   $this->base = 0;
   
   return true;
  
  }
  
  function destroyStack(){
  
   unset($this->stack);
   unset($this->top);
   unset($this->base);
   
   return true;
  
  }
  
  function clearStack(){
  
   for($i = 1; $i <= $this->top; $i++ ){
    
    $this->stack[$i][”content”] = “”;
   
   }
  
   $this->top = 0;
   $this->base = 0;
  
  }
  
  function isEmpty(){
  
   if($this->top == $this->base)
    return true;
   else
    return false;
  
  }
  
  function getTop(){
  
   if($this->top == $this->base)
    return false;
    
   return $this->stack[$this->top][”content”];
  
  }
  
  function push($node){
  
   $this->top += 1;
   $this->stack[$this->top][”content”] = $node;
   
   return true;
   
  }
  
  function pop(){
  
   if($this->top == $this->base){

    return false;
    
   }else{

    $temp = $this->stack[$this->top][”content”];
    
    unset($this->stack[$this->top]);
    
    $this->top -= 1;
    
    return $temp;
   
   }
   
  }
  
  function stackLength(){
  
   return ($this->top - $this->base);
   
  }
  
 }
 
 
?>

作者: php华南培训   发布时间: 2010-02-01

相关阅读 更多

热门下载

更多