<?PHP/** * User:jifei * date:2013-07-30 * time:23:12*//** * PHP implements two-way queue, double-ended queue * Double-ended queue (deque, full name double-ended queue) is a data structure with queue and stack properties. * Elements in a double-ended queue can be ejected from both ends, and insert and delete operations are scoped to both sides of the queue. */classdeque{ Public $queue=Array(); /** * Constructor Initialization queue*/ Public function__construct ($queue=Array()) { if(Is_array($queue)) { $this->queue=$queue; } } /** * Get the first element*/ Public functionFront () {return Reset($this-queue); } /** * Get last element*/ Public functionBack () {return End($this-queue); } /** * To determine if it is empty*/ Public functionIs_empty () {return Empty($this-queue); } /** * Queue size*/ Public functionsize () {return Count($this-queue); } /** * Insert to Tail*/ Public functionPush_back ($val) { Array_push($this->queue,$val); } /** * Insert to head*/ Public functionPush_front ($val) { Array_unshift($this->queue,$val); } /** * Remove the last element*/ Public functionPop_back () {return Array_pop($this-queue); } /** * Remove the first element*/ Public functionPop_front () {return Array_shift($this-queue); } /** * Empty queue*/ Public functionClear () {$this->queue=Array(); }}//initialize a two-way queue$deque=NewDeque (Array(1,2,3,4,5));Echo $deque->size ().Php_eol;Echo $deque->is_empty ().Php_eol;Echo $deque->front ().Php_eol;Echo $deque->back ().Php_eol;Echo Php_eol;//pop-up element testEcho $deque->pop_back ().Php_eol;Echo $deque->pop_front ().Php_eol;Echo $deque->size ().Php_eol;Echo Php_eol;$deque->push_back (' a ').Php_eol;$deque->push_front (0).Php_eol;Echo Php_eol;//Insert TestEcho $deque->front ().Php_eol;Echo $deque->back ().Php_eol;Echo $deque->size ().Php_eol;Echo Php_eol;//Empty Test$deque-Clear ();Echo $deque->is_empty ();
PHP bidirectional queue, double-ended queue code