1 回答
TA贡献1779条经验 获得超6个赞
我不认为您可以使用纯 PHP 来模仿该动画,至少这并不容易。您可以通过在每个步骤打印列表内容来以某种方式为列表内容的输出设置动画,并使用sleep以便能够观察输出的更改:
<?php
$dll = new \SplDoublyLinkedList();
// add 200 to the list using push. Unshift has the same effect because the list is empty
$dll->push(200);
output($dll);
// insert 100 at the beginning of the list
$dll->unshift(100);
output($dll);
// add 34 the end of the list
$dll->push(34);
output($dll);
// add 35 the end of the list
$dll->push(35);
output($dll);
// insert 3 on the second position (usually a loop to find the index would be necessary)
$dll->add(2, 3);
output($dll);
// insert 670 at the beginning of the list
$dll->unshift(670);
output($dll);
// add 450 on the third position
$dll->add(3, 450);
output($dll);
// remove last element of the list
$dll->pop();
output($dll);
// remove first element of the list
$dll->shift();
output($dll);
// remove from position 1 (second linked list element)
$dll->offsetUnset(1);
output($dll);
function output(&$dll) {
ob_start();
$dll->rewind();
$values = [];
while ($dll->valid()) {
$values[] = $dll->current();
$dll->next();
}
echo "[ " . implode(' , ', $values) . " ] \n"; //on the browser change \n to <br>
ob_end_flush();
//ob_flush(); // enable on the browser
flush();
sleep(1); // wait one second
}
- 1 回答
- 0 关注
- 139 浏览
添加回答
举报