In this paper, we introduce the definition and usage of PHP doubly linked list, involving PHP using two-way Chain List class package bidirectional list definition, read, delete, insert and other related operation skills, need friends can refer to, hope to help everyone.
Because a set of data needs to be moved more than once, a doubly linked list is written. But the PHP is not familiar with, although testing each method is not a problem, is not aware of the PHP language deep in these pointers and unset have any attention to the place, put out to let everyone educate it. Efficiency not tested .... Ask for Understanding ~
<?php/** * * * * * * * @author zhiyuan12@ *//** * Linked list element node class */class Node_element {public $pre = NULL;//Precursor public $next = NULL; Subsequent public $key = NULL; Element key value Public $data = NULL; Node value function __construct ($key, $data) {$this->key = $key; $this->data = $data; }}/** * Bidirectional List class */class doublelinkedlist {private $head;//head pointer private $tail;//tail pointer private $current;//Current Pointer private $len; List length function __construct () {$this->head = Self::_getnode (null, NULL); $this->curelement = $this->head; $this->tail = $this->head; $len = 0; }/** * @ desc: Read all nodes of the list */Public Function ReadAll () {$tmp = $this->head; while ($tmp->next!== null) {$tmp = $tmp->next; Var_dump ($tmp->key, $tmp->data); }} Public function Move ($pos 1, $pos 2) {$pos 1Node = $this->findposition ($pos 1); $pos 2Node = $this->findposition ($pos 2); if ($pos 1Node!== null && $pos 2Node!== null) {$tMpkey = $pos 1node->key; $tmpData = $pos 1node->data; $pos 1node->key = $pos 2node->key; $pos 1node->data = $pos 2node->data; $pos 2node->key = $tmpKey; $pos 2node->data = $tmpData; return true; } return false; }/** * @ desc: Delete node in specified keyword * * @param: $key * List element at specified location key */Public Function Delete ($key) {$pos = $t His->find ($key); if ($pos!== null) {$tmp = $pos; $last = null; $first = true; while ($tmp->next!== null && $tmp->next->key = = = $key) {$tmp = $tmp->next; if (! $first) {$this->delnode ($last); } else {$first = false; } $last = $tmp; if ($tmp->next!== null) {$pos->pre->next = $tmp->next; $tmp->next->pre = $pos->pre; } else {$pos->pre->next = null; } $this->delnode ($pos); $this->delnode ($tmp); } } /* * @ desc: Delete node at specified location * * @param: $key * List element at specified location key */Public Function deleteposition ($pos) {$tmp = $this->findposition ($pos); if ($tmp = = = null) {return true; if ($tmp = = = $this->gettail ()) {$tmp->pre->next = null; $this->delnode ($tmp); return true; } $tmp->pre->next = $tmp->next; $tmp->next->pre = $tmp->pre; $this->delnode ($tmp); /** * @ desc: Inserts a node before specifying a key value * * @param: $key *//list element of the specified position key * @param: $data *//list element data to be inserted * @p Aram: $flag *//whether sequential find location is inserted */Public function Insert ($key, $data, $flag = True) {$newNode = Self::_getnode ($key, $data); $tmp = $this->find ($key, $flag); if ($tmp!== null) {$newNode->pre = $tmp->pre; $newNode->next = $tmp; $tmp->pre = $newNode; $newNode->pre->next = $newNode; } else {$newNode->pre = $this->tail; $this->tail->next= $newNode; $this->tail = $newNode; } $this->len + +; /** * @ desc: Insert node before specified position * * @param: $pos * Specify the position of the inserted list * @param: $key * The list element of the specified position key * @param: $data * List element data to be inserted */Public function insertposition ($pos, $key, $data) {$newNode = Self::_getnode ($key, $ data); $tmp = $this->findposition ($pos); if ($tmp!== null) {$newNode->pre = $tmp->pre; $newNode->next = $tmp; $tmp->pre = $newNode; $newNode->pre->next = $newNode; } else {$newNode->pre = $this->tail; $this->tail->next = $newNode; $this->tail = $newNode; } $this->len + +; return true; /** * @ desc: Queries the specified location data according to the key value * * @param: $key *//list element of the specified position key * @param: $flag *//whether to search sequentially */P ublic function Find ($key, $flag = True) {if ($flag) {$tmp = $this->head; while ($tmp->next!== null) {$tmp = $tmp->next; if ($tmp->key = = = $key) {return $tmp; }}}} else {$tmp = $this->gettail (); while ($tmp->pre!== null) {if ($tmp->key = = = $key) {return $tmp; } $tmp = $tmp->pre; }} return null; /** * @ desc: Specify location data based on location query * * @param: $pos *//list element at specified location key */Public Function findposition ($pos) { if ($pos <= 0 | | $pos > $this->len) return null; if ($pos < ($this->LEN/2 + 1)) {$tmp = $this->head; $count = 0; while ($tmp->next!== null) {$tmp = $tmp->next; $count + +; if ($count = = = $pos) {return $tmp; }}}} else {$tmp = $this->tail; $pos = $this->len-$pos + 1; $count = 1; while ($tmp->pre!== null) {if ($count = = = $pos) {return $tmp; } $tmp = $tmp->pre; $count + +; }} return null; }/** * @ desc: Return to the linked header node */publicfunction GetHead () {return $this->head->next; }/** * @ desc: Returns the link footer node */Public Function GetTail () {return $this->tail; /** * @ desc: Query List node number */Public function GetLength () {return $this->len; } private static function _getnode ($key, $data) {$newNode = new node_element ($key, $data); if ($newNode = = = null) {echo "new node fail!"; } return $newNode; } Private Function Delnode ($node) {unset ($node); $this->len--; }} $myList = new Doublelinkedlist (), $myList->insert (1, "test1"), $myList->insert (2, "test2"); $myList->insert ("2b", "Test2-b"), $myList->insert (2, "test2-c"), $myList->insert (3, "test3"), $myList->insertposition (5, "T", "TESTT"), $myList->readall (), echo "+ + +", $myList->deleteposition (0), $myList->readall (), echo "...". $myList->getlength (); Var_dump ($myList->findposition (3)->data);? >
Operation Result:
Int (1) string (5) "Test1" Int (2) string (7) "Test2-c" Int (2) string (5) "Test2" string (2) "2b" string (7) "Test2-b" string (1) "T "String (5)" Testt "int (3) string (5)" Test3 "+++int (1) string (5)" Test1 "Int (2) string (7)" Test2-c "Int (2) string (5)" Test2 " String (2) "2b" string (7) "Test2-b" string (1) "T" string (5) "Testt" int (3) string (5) "Test3" ... 6string (5) "Test2"