1 回答

TA貢獻1779條經(jīng)驗 獲得超6個贊
我不認為您可以使用純 PHP 來模仿該動畫,至少這并不容易。您可以通過在每個步驟打印列表內(nèi)容來以某種方式為列表內(nèi)容的輸出設(shè)置動畫,并使用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 關(guān)注
- 155 瀏覽
添加回答
舉報