<?php
class Node{
public $value; // 节点值
public $nextNode; // 下一个节点
}
function create($node, $value){
$node->value = $value;
}
function addNode($node, $value){
$lastNode = findLastNode($node);
$nextNode = new Node();
$nextNode->value = $value;
$lastNode->nextNode = $nextNode;
}
/* 找到最后的节点 */
function findLastNode($node){
if(empty($node->nextNode)){
return $node;
}else{
return findLastNode($node->nextNode);
}
}
/* 删除节点 必须head为引用传值 */
function deleteNode(&$head, $node, $m, $k = 1){
if($k + 1 == $m){
if($node->nextNode == $head){
$node->nextNode = $node->nextNode->nextNode;
$head = $node->nextNode;
return $node->nextNode;
}else{
$node->nextNode = $node->nextNode->nextNode;
return $node->nextNode;
}
}else{
return deleteNode($head, $node->nextNode, $m, ++$k);
}
}
/* 节点数 */
function countNode($head, $node, $count = 1){
if($node->nextNode == $head){
return $count;
}else{
return countNode($head, $node->nextNode, ++$count);
}
}
function printNode($head, $node){
echo $node->value . ' ';
if($node->nextNode == $head) return;
printNode($head, $node->nextNode);
}
function show($data){
echo '<pre>';
print_r($data);
echo '</pre>';
}
$head = new Node();
create($head, 1);
addNode($head, 2);
addNode($head, 3);
addNode($head, 4);
addNode($head, 5);
addNode($head, 6);
addNode($head, 7);
addNode($head, 8);
addNode($head, 9);
addNode($head, 10);
addNode($head, 11);
addNode($head, 12);
$lastNode = findLastNode($head);
$lastNode->nextNode = $head;
$count = countNode($head, $head);
$tmpHead = $head;
while ($count > 2) {
$tmpHead = deleteNode($head, $tmpHead, 3, 1);
$count = countNode($head, $head);
}
printNode($head, $head);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有