主旨:主要是考數組的函數
array_pop array_push
array_pop array_pop() 函數刪除數組中的最後一個元素。刪除尾部一 array_push array_push() 函數向第一個參數的數組尾部添加一個或多個元素(入棧),然後返回新數組的長度。 該函數等於多次調用 $array[] = $value。 尾部塞入一
array_unshift array_shift
array_shift() 函數刪除數組中第一個元素,並返回被刪除元素的值。 刪除頭第一 array_unshift() 函數用於向數組插入新元素。新數組的值將被插入到數組的開頭。 插入頭第一
reset end
reset reset() 函數將內部指標指向數組中的第一個元素,並輸出。 end end() 函數將數組內部指標指向最後一個元素,並返回該元素的值(如果成功)。
實現代碼:
<?phpclass Deque { public $queue = array(); /**(尾部)入隊 **/ public function addLast($value) { return array_push($this->queue,$value); } /**(尾部)出隊**/ public function removeLast() { return array_pop($this->queue); } /**(頭部)入隊**/ public function addFirst($value) { return array_unshift($this->queue,$value); } /**(頭部)出隊**/ public function removeFirst() { return array_shift($this->queue); } /**清空隊列**/ public function makeEmpty() { unset($this->queue); } /**擷取列頭**/ public function getFirst() { return reset($this->queue); } /** 擷取列尾 **/ public function getLast() { return end($this->queue); } /** 擷取長度 **/ public function getLength() { return count($this->queue); } }