PHP uses circular linked list to find monkey king

PHP uses circular linked list to find monkey king

1. As mentioned above

1.1 Implementation description:
The implementation idea is similar to that of the array array, but the implementation method of the linked list is slightly cumbersome.

1.2 Implementation ideas:
build aone-way circular linked listContinuously search in a loop, find the designated deletion position, and kick out the monkey. The kicked out monkey is stored in an array.
PHP can actually be implemented in this way, and the effect is no different from array implementation, which is quite surprising to me who doesn't know PHP...

1.3 Realize the effect
insert image description here
insert image description here

2.php source code

<?php
echo "<pre>";
/*
    n-b
    思路:利用单向循环链表实现选猴王
*/
class Node
{
    
    
    public $ID; //编号
    public $next; //下一猴子
    public function __construct($ID = 0, $next = null)
    {
    
    
        $this->ID  = $ID;
        $this->next = $next;
    }
}

class LinkedList
{
    
    
    public $head; //头结点
    public $curr; //当前猴
    public $curr_counts; //当前猴个数
    public $out_monkey; //被提出圈子的猴

    //构造函数
    public function __construct()
    {
    
    
        $this->head = new Node();
        $this->curr = $this->head;
    }

    //倒序头插法:猴编号
    public function head_insert($monkey_total)
    {
    
    
        for ($i = $monkey_total; $i >= 1; $i--) {
    
    
            if ($i == $monkey_total) {
    
    
                $this->head->next = new Node($i, $this->head); //构成循环
                ++$this->curr_counts;
            } else {
    
    
                $this->head->next = new Node($i, $this->head->next); //正常添加
                ++$this->curr_counts;
            }
        }
    }
    
    //把猴踢出去
    public function delete_node($out_index)
    {
    
    
        $this->out_monkey = array(); //分配数组,存放被删除的猴
        while ($this->curr_counts > 1) {
    
    
            $index = $this->curr; //开始计数位置
            for ($i = 0; $i < $out_index; $i++) {
    
    
                //跳过头结点
                if ($index->next == $this->head)
                    $index = $index->next;

                if ($i == ($out_index - 1)) {
    
    
                    --$this->curr_counts;
                    $temp = $index->next->next;
                    //跳过头结点
                    if ($temp == $this->head)
                        $temp = $temp->next;
                    array_push($this->out_monkey, $index->next->ID); //保存被踢出的猴子
                    unset($index->next); //销毁被踢出的猴子
                    $index->next = $temp; //重新链接
                    $this->curr = $index; //保存当前值
                }
                $index = $index->next;
            }
        }
    }

    public function start_game($monkey_total, $out_index)
    {
    
    
        //对输入的数据检错
        if (is_numeric($monkey_total) && is_numeric($out_index)) {
    
    
            if ($monkey_total < 1 || $out_index < 1) {
    
    
                print("\n输入参数错误!");
                return;
            }
        } else {
    
    
            print("\n输入参数错误!");
            return;
        }

        //只有一只猴子情况
        if ($monkey_total == 1) {
    
    
            printf("\n猴子总数:%d\n猴王编号:%d\n被提出圈的猴子:%d,因为只有一只猴,它就是猴王。", $monkey_total, $monkey_total, 0);
        } else {
    
    
            $this->head_insert($monkey_total); //猴子编号
            $this->delete_node($out_index); //开始淘汰猴子

            printf("\n猴子总数:%d\n猴王编号:%d", $monkey_total, $this->curr->ID);
            print("\n被踢出圈的猴子:");
            print_r($this->out_monkey);

            //重置,以便下次继续
            $this->curr = $this->head;
            unset($this->head->next);
            unset($this->out_monkey);
        }
    }
}
$monkey = new LinkedList();

/* 说明:有环境才可以跑 */
$monkey_total = $_POST["monkey_total"];
$out_index = $_POST["out_index"];
$monkey->start_game($monkey_total, $out_index);

/* 无本地环境注释上面的,去在线php编译器跑下面的,数据自行输入 */
// $monkey->start_game(5, 3);
// $monkey->start_game(1, 1);
// $monkey->start_game(10, 8);

3. html source code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>找猴王</title>
    <style>
        body {
      
      
            text-align: center;
        }
        table {
      
      
            margin: auto;
            margin-top: -15px;
            text-align: center;
        }
    </style>
</head>

<body>
    <table border="25">
        <h4>
            <font size="6">找猴王</font>
            <br>
            输入数据范围:[1,n]
        </h4>
        <form method="post" enctype="multipart/form-data"
            action="http://localhost:3000/php/find_monkey_king.php">
            <tr>
                <td>猴 子 总 数</td>
                <td><input type="text" name="monkey_total"></td>
            </tr>
            <tr>
                <td>数到第几只淘汰</td>
                <td><input type="out_index" name="out_index"></td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="submit" value="开始找猴王">
                    <input type="reset" value="重置此输入">
                </td>
            </tr>
        </form>
    </table>
</body>
</html>

Guess you like

Origin blog.csdn.net/CSDN_Yuanyuan/article/details/127686208