Home >Backend Development >PHP Tutorial >Implement a two-way queue
class deque{ public $queue = array(); public $length = 0; public function rpop(){ $node = array_pop($this->queue); $this->countque(); return $node; } public function rpush($node){ array_push($this->queue, $node); $this->countque(); return $this->queue; } public function lpop(){ $node = array_shift($this->queue); $this->countque(); return $node; } public function lpush($node){ array_unshift($this->queue, $node); $this->countque(); return $this->queue; } private function countque(){ $this->length = count($this->queue); } }
Redis implements its own double-ended linked list structure.
•The double-ended linked list has two main functions: ◦As one of the underlying implementations of the Redis list type;
◦As a general data structure, used by other functional modules;
•The performance characteristics of the double-ended linked list and its nodes are as follows: ◦The node has predecessor and successor pointers, the complexity of accessing the predecessor node and successor node is O(1), and the iteration of the linked list can be carried out in two directions, from the head to the tail and from the tail to the head;
◦The linked list has pointers to the head and tail of the table, so the complexity of processing the head and tail is O(1);
◦The linked list has an attribute that records the number of nodes, so it can be processed in O(1) The number of nodes (length) returned in the linked list within the degree;