php 队列、堆栈类V1.1

转帖:http://blog.csdn.net/acsheva/archive/2006/02/24/608466.aspx

//更新


//当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);
   
  }
  
 }
 
 
?>

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值