Home > Backend Development > PHP Tutorial > Implement a two-way queue

Implement a two-way queue

巴扎黑
Release: 2016-11-22 11:36:06
Original
1183 people have browsed it

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);
}
}
Copy after login

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;

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template