隊列是一種線性表,按照先進先出的原則進行的:
看看各語言實現隊列的方法:
PHP實現隊列:第一個元素做爲隊頭,最後一個元素做爲隊尾php
- <?php
- /**
- * 隊列就是這麼簡單
- *
- * @link http://www.phpddt.com
- */
- $array = array('PHP', 'JAVA');
-
- array_push($array, 'PYTHON'); //入隊列
-
- array_shift($array); //出隊列
什麼是雙端隊列(或雙向隊列)Deque,全名double-ended queue?
即元素能夠在隊列的任意一段入隊或出隊,若是咱們把這些方法叫作insertLeft()和insertRight(),以及removeLeft()和removeRight()。若是嚴格禁止調用insertLeft()和removeLeft()方法(或禁用右段的操做),雙端隊列功能就和棧同樣。禁止調用insertLeft()和removeRight()(或相反的另外一對方法),它的功能就和隊列同樣了。雙端隊列與棧或隊列相比,是一種多用途的數據結構。
PHP實現雙端隊列:html
- <?php
- class 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);
- }
- }
隊列的用途:
隊列能夠很好地異步處理數據傳送和存儲,當你頻繁地向數據庫中插入數據、頻繁地向搜索引擎提交數據,就可採起隊列來異步插入。另外,還能夠將較慢的處理邏輯、有併發數量限制的處理邏輯,經過消息隊列放在後臺處理,例如FLV視頻轉換、發送手機短信、發送電子郵件等。數據庫