如何使用 SplDoublyLinkedList 模仿这个双向链表动画?

我试图对使用此工具生成的双向链表动画进行逆向工程:

http://img4.mukewang.com/618f9cd90001429608460318.jpg

所以我试过这个:


<?php


$dll = new \SplDoublyLinkedList();


$dll->unshift(200);//inserir no início

$dll->unshift(100);//inserir no início

$dll->push(34);//inserir no final

$dll->push(35);//inserir no final

$dll->add(2, 3); //inserir em posicao específica

$dll->unshift(670);//inserir no início

$dll->add(($dll->count() / 2)-1, 450);//inserir no meio

$dll->pop(); //remover do final

$dll->shift(); //remover do início

$dll->offsetUnset(1);//remover de posicao específica


$prev = null;

$dll->rewind(); //rebobinando

while ($dll->valid()) {

    $current = $dll->current();

    echo 'Atual: '.$current, "\n";

    $dll->next();

}

但是结果与动画不同:(如何模仿这个双向链表动画并获得相同的结果?


慕勒3428872
浏览 140回答 1
1回答

哆啦的时光机

我不认为您可以使用纯 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);&nbsp;output($dll);// add 34 the end of the list$dll->push(34);&nbsp;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&nbsp;$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);&nbsp;output($dll);function output(&$dll) {&nbsp; &nbsp; ob_start();&nbsp; &nbsp; $dll->rewind();&nbsp; &nbsp; $values = [];&nbsp; &nbsp; while ($dll->valid()) {&nbsp; &nbsp; &nbsp; &nbsp; $values[] = $dll->current();&nbsp; &nbsp; &nbsp; &nbsp; $dll->next();&nbsp; &nbsp; }&nbsp; &nbsp; echo "[ " . implode(' , ', $values) . " ] \n"; //on the browser change \n to <br>&nbsp; &nbsp; ob_end_flush();&nbsp; &nbsp; //ob_flush(); // enable on the browser&nbsp; &nbsp; flush();&nbsp; &nbsp; sleep(1); // wait one second}
打开App,查看更多内容
随时随地看视频慕课网APP