PHP member ranking function example based on two-way linked list and sorting operation, php example
This article describes the membership ranking function of PHP based on two-way linked list and sorting operation. We will share this with you for your reference. The details are as follows:
A two-way linked list is also called a double-chain table, which is a type of linked list. Each of its data nodes has two pointers pointing to the direct successor and direct precursor respectively. Therefore, starting from any node in the two-way linked list, you can easily access its precursor node and successor node. If you do not know the concept, Baidu.
<? Php/*** ** is only used to reflect the ideological logic, no practical reference value * @ author crazy old driver * @ date 2016-07-07 */class Rank {/*** @ var refers to the reference of the previous node */public $ pre = null; /*** @ var points to the reference of the next node */public $ next = null;/*** @ var User ranking id */public $ id; /*** @ var user name */public $ username; public function _ construct ($ id = '', $ username = '') {$ this-> id = $ id; $ this-> username = $ username;}/*** add member Node Method ** @ access public * @ param Obj head initial node * @ param obj rank Member node */public static function addRank ($ head, $ rank) {$ cur = $ head; // secondary node $ isExist = false; // This is a flag bit while ($ cur-> next! = Null) {if ($ cur-> next-> id> $ rank-> id) {break ;} else if ($ cur-> next-> id ==$ rank-> id) {$ isExist = true; echo '<br/> you cannot add the same id ';} $ cur = $ cur-> next;} if (! $ IsExist) {if ($ cur-> next! = Null) {$ rank-> next = $ cur-> next;} $ rank-> pre = $ cur; if ($ cur-> next! = Null) {$ cur-> next-> pre = $ rank;} $ cur-> next = $ rank ;}} /*** Method for deleting a member node ** @ access public * @ param obj head initial node * @ param obj rankid User ranking id */public static function delRank ($ head, $ rankid) {$ cur = $ head-> next; $ isFind = flase; // tag bit while ($ cur! = Null) {if ($ cur-> id = $ rankid) {$ isFind = true; break;} $ cur = $ cur-> next;} if ($ isFind) {if ($ cur-> next! = Null) {$ cur-> next-> pre = $ cur-> pre;} $ cur-> pre-> next = $ cur-> next; echo '<br/> the member id to be deleted is '. $ cur-> id;} else {echo '<br/> the member to be deleted does not have ';}} /*** traverse all nodes and display the output ** @ access public * @ param obj head initial node */public static function showRank ($ head) {$ cur = $ head-> next; // do not print an empty node while ($ cur-> next! = Null) {echo '<br/> id = '. $ cur-> id. ''. 'username = '. $ cur-> username; $ cur = $ cur-> next;} echo '<br/> id = '. $ cur-> id. ''. 'username = '. $ cur-> username ;}/// create an initial node $ head = new Rank (); // create a member $ rank = new Rank (1, 'old king '); rank: addRank ($ head, $ rank); $ rank = new Rank (2, 'xiaoming '); Rank: addRank ($ head, $ rank ); $ rank = new Rank (6, 'Big bear '); Rank: addRank ($ head, $ rank); $ rank = new Rank (3, 'jingxiang'); Rank:: addRank ($ head, $ rank); $ rank = new Rank (56, 'Sun Erniang '); Rank: addRank ($ head, $ rank); echo' <br/> member ranking ..... '; Rank: showRank ($ head); echo' <br/> '; echo' <br/> ranking of deleted members ..... '; Rank: delRank ($ head, 3); Rank: showRank ($ head); echo' <br/> '; echo '<br/> test to delete the first and last members <br/>'; echo '<br/> the list of deleted members ..... '; Rank: delRank ($ head, 1); Rank: showRank ($ head); echo' <br/> '; echo '<br/> member ranking after deletion ..... '; Rank: delRank ($ head, 56); Rank: showRank ($ head);?>
Running result:
Member ranking ..... id = 1 username = Lao Wang id = 2 username = Xiao Ming id = 3 username = Jing Xiang id = 6 username = Da Xiong id = 56 username = List of members deleted by Sun Erniang ..... the member id to be deleted is 3id = 1 username = Old Wang id = 2 username = Xiaoming id = 6 username = bear id = 56 username = sun Erniang. Test to delete the first and last members. removed member ranking ..... the member id to be deleted is 1id = 2 username = Xiao Ming id = 6 username = Da Xiong id = 56 username = sun Erniang's member ranking after deletion ..... the member id to be deleted is 56id = 2 username = Xiaoming id = 6 username = bear