Example of member ranking function implemented by PHP based on bidirectional linked list and sorting operation
- 2021-08-31 07:31:39
- OfStack
This article describes the PHP based on two-way linked list and sorting operations to achieve the member ranking function. Share it for your reference, as follows:
Two-way linked list is also called double linked list, which is a kind of linked list. There are two pointers in each data node, pointing to direct successor and direct precursor respectively. Therefore, starting from any one node in the bidirectional linked list, it is convenient to access its predecessor node and successor node. If you don't know the concept, please Baidu yourself.
<?php
/**
* Realization of User Ranking List by Double Linked List
*
* It is only used to reflect ideological logic and has no practical reference value
* @author Crazy old driver
* @date 2016-07-07
*/
class Rank{
/**
* @var Point forward 1 Reference to nodes
*/
public $pre = null;
/**
* @var Pointing back 1 Reference to nodes
*/
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; // Auxiliary node
$isExist = false; // This is 1 Flag bits
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;
}
}
/**
* Delete member node method
*
* @access public
* @param obj head Initial node
* @param obj rankid User ranking id
*/
public static function delRank($head, $rankid){
$cur = $head->next;
$isFind = flase; // Marker 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/> Members to delete id Yes '.$cur->id;
}else{
echo'<br/> The member to delete does not have ';
}
}
/**
* Traverse all nodes and output display
*
* @access public
* @param obj head Initial node
*/
public static function showRank($head){
$cur = $head->next; // Do not print empty nodes
while($cur->next != null){
echo'<br/>id='.$cur->id.' '.'username='.$cur->username;
$cur = $cur->next;
}
echo'<br/>id='.$cur->id.' '.'username='.$cur->username;
}
}
// Create 1 Initial nodes
$head=new Rank();
// Create 1 Members
$rank=new Rank(1,' Lao Wang ');
Rank::addRank($head,$rank);
$rank=new Rank(2,' Xiao Ming ');
Rank::addRank($head,$rank);
$rank=new Rank(6,' Big bear ');
Rank::addRank($head,$rank);
$rank=new Rank(3,' Shizuka ');
Rank::addRank($head,$rank);
$rank=new Rank(56,' Sun 2 Mother ');
Rank::addRank($head,$rank);
echo '<br/> Membership list .....';
Rank::showRank($head);
echo'<br/>';
echo '<br/> Deleted member list .....';
Rank::delRank($head,3);
Rank::showRank($head);
echo'<br/>';
echo'<br/> The following test deletes the front and back members <br/>';
echo '<br/> Deleted member list .....';
Rank::delRank($head,1);
Rank::showRank($head);
echo'<br/>';
echo '<br/> Deleted member list .....';
Rank::delRank($head,56);
Rank::showRank($head);
?>
Run results:
Membership list .....
id=1 username= Lao Wang
id=2 username= Xiao Ming
id=3 username= Shizuka
id=6 username= Big bear
id=56 username= Sun 2 Mother
Deleted member list .....
Members to delete id Yes 3
id=1 username= Lao Wang
id=2 username= Xiao Ming
id=6 username= Big bear
id=56 username= Sun 2 Mother
The following test deletes the front and back members
Deleted member list .....
Members to delete id Yes 1
id=2 username= Xiao Ming
id=6 username= Big bear
id=56 username= Sun 2 Mother
Deleted member list .....
Members to delete id Yes 56
id=2 username= Xiao Ming
id=6 username= Big bear
For more readers interested in PHP related contents, please check the special topics of this site: "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary", "php String (string) Usage Summary", "PHP Array (Array) Operation Skills Complete Book", "PHP Common Traversal Algorithms and Skills Summary" and "PHP Mathematical Operation Skills Summary"
I hope this article is helpful to everyone's PHP programming.