285572001 发表于 2017-7-2 14:23:10

wangnan

<?php  class StackQueue
  {
  public $stack_a;
  public $stack_b;
  public function __construct()
  {
  $this->stack_a = new SplStack();
  $this->stack_b = new SplStack();
  }
  public function pop()
  {
  $this->exchange($this->stack_a, $this->stack_b);
  $pop = $this->stack_b->pop();
  $this->exchange($this->stack_b, $this->stack_a);
  return $pop;
  }
  public function push($value)
  {
  $push = $this->stack_a->push($value);
  return $push;
  }
  public function exchange($stack_a, $stack_b)
  {
  $count = $stack_a->count();
  for ($i = 0; $i < $count; $i++) {
  $pop = $stack_a->pop();
  $stack_b->push($pop);
  }
  }
  }
  $StackQueue = new StackQueue();
  $StackQueue->push(1);
  $StackQueue->push(2);
  echo $StackQueue->pop();
  echo $StackQueue->pop();
页: [1]
查看完整版本: wangnan