English Sentence Loading...
英语句子加载中...
  • 1 
预览模式: 普通 | 列表

用php实现一个双向队列

代码实现:
下载: deque.php
  1. <?php
  2.     class deque
  3.     {
  4.         public $queue  = array();
  5.         public $length = 0;
  6.        
  7.         public function frontAdd($node){
  8.             array_unshift($this->queue,$node);
  9.             $this->countqueue();
  10.         }
  11.        
  12.         public function frontRemove(){
  13.             $node = array_shift($this->queue);
  14.             $this->countqueue();
  15.             return $node;
  16.         }
  17.        
  18.         public function rearAdd($node){
  19.             array_push($this->queue,$node);
  20.             $this->countqueue();
  21.         }
  22.        
  23.         public function rearRemove(){
  24.             $node = array_pop($this->queue);
  25.             $this->countqueue();
  26.             return $node;
  27.         }
  28.        
  29.         public function countqueue(){
  30.             $this->length = count($this->queue);   
  31.         }
  32.     }
  33. ?>


这道题从难度上讲其实不是很难,它主要考察了phper以下几个方面的技能:
1. 当然是双向队列的定义,这个就不多做解释了。
2. 考察对函数是否熟悉。
3. 考察OOP编程。
4. 考察程序员的代码规范和编程习惯。
标签: php 数据结构

数据结构 之 直接插入排序

  1. <?php
  2.     $array = array(13,15,6,10,20,6,3,19);
  3.     $count = count($array);
  4.     $n = $e = 0;
  5.     for($i = 1; $i < $count; $i++)
  6.     {
  7.         $tmp = $array[$i];
  8.         $j = $i;
  9.         while($array[$j-1] > $tmp){
  10.             $n++;
  11.             $e++;
  12.             $array[$j] = $array[$j-1];
  13.             $j--;
  14.         }
  15.         $array[$j] = $tmp;
  16.     }
  17.     print_r($array);
  18.     echo "共循环{$n}次,共执行{$e}次数据交换";
  19. ?>


执行结果为:
  1. Array ( [0] => 3 [1] => 6 [2] => 6 [3] => 10 [4] => 13 [5] => 15 [6] => 19 [7] => 20 )
  2. 共循环15次,共执行15次数据交换

数据结构 之 冒泡法排序

  1. <?php
  2.     $array = array(13,15,6,10,20,6,3,19);
  3.     $count = count($array);
  4.     $n = $e = 0;
  5.     for($i = 0; $i < $count; $i++)
  6.     {
  7.             for($j = $i + 1; $j < $count; $j++)
  8.             {
  9.                 $n++;
  10.                 if($array[$j] < $array[$i]){
  11.                     $e++;
  12.                     $tmp = $array[$i];
  13.                     $array[$i] = $array[$j];
  14.                     $array[$j] = $tmp;  
  15.                 }  
  16.             }
  17.     }
  18.     print_r($array);
  19.     echo "共循环{$n}次,共执行{$e}次数据交换";
  20. ?>

执行结果为:
  1. Array ( [0] => 3 [1] => 6 [2] => 6 [3] => 10 [4] => 13 [5] => 15 [6] => 19 [7] => 20 )
  2. 共循环28次,共执行14次数据交换

数据结构 之 简单选择排序

  1. <?php
  2.     $array = array(13,15,6,10,20,6,3,19);
  3.     $count = count($array);
  4.     $n = $e = 0;
  5.     for($i = 1; $i < $count; $i++)
  6.     {
  7.         //find the min item from $i to last
  8.         $min_index = $i;
  9.         for($j = $i+1; $j < $count; $j++)
  10.         {
  11.             $n++;
  12.             if($array[$j] < $array[$min_index]) $min_index = $j;
  13.         }
  14.       
  15.         if($array[$min_index] < $array[$i-1])
  16.         {
  17.             $e++;
  18.         $tmp = $array[$i-1];
  19.         $array[$i-1] = $array[$min_index];
  20.         $array[$min_index] = $tmp;
  21.         }
  22.     }
  23.     print_r($array);
  24.     echo "共循环{$n}次,共执行{$e}次数据交换";
  25. ?>


执行结果为:
  1. Array ( [0] => 3 [1] => 6 [2] => 6 [3] => 10 [4] => 13 [5] => 15 [6] => 19 [7] => 20 )
  2. 共循环21次,共执行5次数据交换
  • 1