English Sentence Loading...
英语句子加载中...
用php实现一个双向队列
作者:蘑菇 日期:2010-07-23 00:54
代码实现:
这道题从难度上讲其实不是很难,它主要考察了phper以下几个方面的技能:
1. 当然是双向队列的定义,这个就不多做解释了。
2. 考察对函数是否熟悉。
3. 考察OOP编程。
4. 考察程序员的代码规范和编程习惯。
下载: deque.php
- <?php
- class deque
- {
- public $queue = array();
- public $length = 0;
- public function frontAdd($node){
- array_unshift($this->queue,$node);
- $this->countqueue();
- }
- public function frontRemove(){
- $node = array_shift($this->queue);
- $this->countqueue();
- return $node;
- }
- public function rearAdd($node){
- array_push($this->queue,$node);
- $this->countqueue();
- }
- public function rearRemove(){
- $node = array_pop($this->queue);
- $this->countqueue();
- return $node;
- }
- public function countqueue(){
- $this->length = count($this->queue);
- }
- }
- ?>
这道题从难度上讲其实不是很难,它主要考察了phper以下几个方面的技能:
1. 当然是双向队列的定义,这个就不多做解释了。
2. 考察对函数是否熟悉。
3. 考察OOP编程。
4. 考察程序员的代码规范和编程习惯。
数据结构 之 直接插入排序
作者:蘑菇 日期:2010-04-21 02:01
下载: insertorder.php
- <?php
- $array = array(13,15,6,10,20,6,3,19);
- $count = count($array);
- $n = $e = 0;
- for($i = 1; $i < $count; $i++)
- {
- $tmp = $array[$i];
- $j = $i;
- while($array[$j-1] > $tmp){
- $n++;
- $e++;
- $array[$j] = $array[$j-1];
- $j--;
- }
- $array[$j] = $tmp;
- }
- print_r($array);
- echo "共循环{$n}次,共执行{$e}次数据交换";
- ?>
执行结果为:
- Array ( [0] => 3 [1] => 6 [2] => 6 [3] => 10 [4] => 13 [5] => 15 [6] => 19 [7] => 20 )
- 共循环15次,共执行15次数据交换
数据结构 之 冒泡法排序
作者:蘑菇 日期:2010-04-21 01:44
- <?php
- $array = array(13,15,6,10,20,6,3,19);
- $count = count($array);
- $n = $e = 0;
- for($i = 0; $i < $count; $i++)
- {
- for($j = $i + 1; $j < $count; $j++)
- {
- $n++;
- if($array[$j] < $array[$i]){
- $e++;
- $tmp = $array[$i];
- $array[$i] = $array[$j];
- $array[$j] = $tmp;
- }
- }
- }
- print_r($array);
- echo "共循环{$n}次,共执行{$e}次数据交换";
- ?>
执行结果为:
- Array ( [0] => 3 [1] => 6 [2] => 6 [3] => 10 [4] => 13 [5] => 15 [6] => 19 [7] => 20 )
- 共循环28次,共执行14次数据交换
数据结构 之 简单选择排序
作者:蘑菇 日期:2010-04-21 01:38
- <?php
- $array = array(13,15,6,10,20,6,3,19);
- $count = count($array);
- $n = $e = 0;
- for($i = 1; $i < $count; $i++)
- {
- //find the min item from $i to last
- $min_index = $i;
- for($j = $i+1; $j < $count; $j++)
- {
- $n++;
- if($array[$j] < $array[$min_index]) $min_index = $j;
- }
- if($array[$min_index] < $array[$i-1])
- {
- $e++;
- $tmp = $array[$i-1];
- $array[$i-1] = $array[$min_index];
- $array[$min_index] = $tmp;
- }
- }
- print_r($array);
- echo "共循环{$n}次,共执行{$e}次数据交换";
- ?>
执行结果为:
- Array ( [0] => 3 [1] => 6 [2] => 6 [3] => 10 [4] => 13 [5] => 15 [6] => 19 [7] => 20 )
- 共循环21次,共执行5次数据交换
- 1

