php resolves Joseph ring through a circular chain

Source: Internet
Author: User

I want to use PHP to write some data structure to improve, write to the chain when you see Joseph Ring problem, try to use a circular chain to write a bit

Joseph Ring:

Josef Ring (Joseph question) is a mathematical application problem: known n individuals (denoted by number 1,2,3...N) sit around a table. From the number of people who numbered K, the man who counted to M is out of the way, and his next man counts off from 1, and the person counting to M is out of the way; Repeat until all the people around the table are out.

Code:

<?php
Header ("content-type:text/html; Charset=utf-8 ");
/**
* Joseph Ring
*/

/**
* Knot Point
*/
Class Node {
/* Knot Point id*/
public $id;
/* Node data field */
Public $data;
/* The next node's pointer field */
Public $_next;

function __construct ($data) {
$this->id = null;
$this->data = $data;
$this->_next = null;
}
}

/**
* Cycle Chain
*/
Class Circularlinkedlist {
/* List head pointer */
Private $_header;

/* Chain footer pointer */
Private $_end;

/* An array of data for the stored list */
Private $data = Array ();

function __construct ($node) {
$this->_header = 0;
$this->_end = 0;
$node->id = $this->_end;
$node->_next = $this->_header;
$this->data[$this->_end] = $node;
}

/* Add node */
Public Function Add_list ($node) {
$current = $this->data[$this->_header];
while ($current->_next!== $this->_header) {
$current = $this->data[$current->_next];
}

$this->_end++;
$node->id = $this->_end;
$node->_next = $this->_header;
$this->data[$current->id]->_next = $node->id;
$this->data[$this->_end] = $node;
}

/* Insert Node */
Public Function Insert_list ($where, $node) {
if ($where = = 1) {
$current = $this->data[$this->_header];
while ($current->_next!== $this->_header) {
$current = $this->data[$current->_next];
}

$this->_end++;
$node->id = $this->_end;
$node->_next = $this->data[$this->_header]->id;
$this->data[$current->id]->_next = $node->id;
$this->data[$this->_end] = $node;
$this->_header = $node->id;
}
else{
$current = $this->data[$this->_header];
for ($i =1; $i < $where-1; $i + +) {
$current = $this->data[$current->_next];
}

$this->_end++;
$node->id = $this->_end;
$node->_next = $this->data[$current->_next]->id;
$this->data[$current->id]->_next = $node->id;
$this->data[$this->_end] = $node;
}
}

/* Delete the specified node */
Public Function Del_list ($where) {
$current = $this->data[$this->_header];
for ($i =1; $i < $where-1; $i + +) {
$current = $this->data[$current->_next];
}

$next = $this->data[$current->id]->_next;

if ($where = = 1) {
$current _2 = $this->data[$this->_header];
while ($current _2->_next!== $this->_header) {
$current _2 = $this->data[$current _2->_next];
}

$this->_header = $next;
$this->data[$current _2->id]->_next = $next;
unset ($this->data[$current->id]);
}
else{
$this->data[$current->id]->_next = $this->data[$next]->_next;
unset ($this->data[$next]);
}
}

/* Length of loop chain */
Public Function get_length () {
$current = $this->data[$this->_header];
$length = 1;
while ($current->_next!== $this->_header) {
$length + +;
$current = $this->data[$current->_next];
}

return $length;
}

/* Change the head pointer */
Public Function Change_header ($where) {
$current = $this->search_node ($where);

$this->_header = $current->id;
}

/* Find nth Node */
Public Function Search_node ($where) {
$current = $this->data[$this->_header];
for ($i =1; $i < $where; $i + +) {
$current = $this->data[$current->_next];
}

return $current;
}

Public Function Set_header ($header) {
$this->_header = $header;
}

/* Output loop chain */
Public Function get_list () {
$current = $this->data[$this->_header];
while ($current->_next!== $this->_header) {
echo "[". $current->id. "]". $current->data. " ---> ";
$current = $this->data[$current->_next];
}
echo "[". $current->id. "]". $current->data. " --->[". $this->data[$current->_next]->id."]. $this->data[$current->_next]->data;
echo "<br>-----------------------------------------------<br>";
}
}

/* Joseph Ring Object */
Class Josephcycle {

/* Loop chain */
Private $linkedlist;

/* Start position */
Private $_begin;

/* start at the distance to be kicked out */
Private $_distance;

/**
* Constructor function
* @param object $linkedlist loop Chain
* @param int $begin starting from the first
* @param int $distance The distance from the start to the kicker
*/
function __construct ($linkedlist, $begin, $distance) {
$this->linkedlist = $linkedlist;
$this->_begin = $begin;
$this->_distance = $distance;
}

Public Function index () {
$length = $this->linkedlist->get_length ();
$this->linkedlist->change_header ($this->_begin);
for ($i =1; $i < $length; $i + +) {
$node = $this->linkedlist->search_node ($this->_distance);
$this->linkedlist->del_list ($this->_distance);
$this->linkedlist->set_header ($node->_next);
$this->linkedlist->get_list ();
}
}
}

$list = new Circularlinkedlist (New Node ("Test 1"));
$list->add_list (New Node ("Test 2"));
$list->add_list (New Node ("Test 3"));
$list->add_list (New Node ("Test 4"));
$list->add_list (New Node ("Test 5"));
$list->add_list (New Node ("Test 6"));
$list->add_list (New Node ("Test 7"));
$list->add_list (New Node ("Test 8"));
$list->add_list (New Node ("Test 9"));
$list->add_list (New Node ("Test 10"));
$list->add_list (New Node ("Test 11"));
$list->add_list (New Node ("Test 12"));
$list->add_list (New Node ("Test 13"));
$list->get_list ();

$solution = new Josephcycle ($list, 6,8);
$solution->index ();
?>

If there are any mistakes, please correct them and accept them humbly.

php resolves Joseph ring through a circular chain

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.