简体   繁体   English

splstack推送并添加

[英]splstack push and add

How to work out the stack if we use push and add together 如果我们一起使用推和加法,如何计算堆栈

$stack = new SplStack();
$stack[] = 'one2';
$stack[] = 20152;
$stack[] = 'two2';

echo "<pre>";
print_r($stack);

$stack->add(0, 'one');
$stack->add(1, 'two');
$stack->add(2, '2015');


echo "<pre>";
print_r($stack);

I am seeing this in the result but could not get my head to work out the second section. 我在结果中看到了这一点,但无法确定第二部分。

Result of first print_r: 第一个print_r的结果:

SplStack Object
(
    [flags:SplDoublyLinkedList:private] => 6
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => one2
            [1] => 20152
            [2] => two2
        )
)

Result of second print_r: 第二个print_r的结果:

SplStack Object
(
    [flags:SplDoublyLinkedList:private] => 6
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => one2
            [1] => 20152
            [2] => 2015
            [3] => two
            [4] => one
            [5] => two2
        )
)

It took me a while to get my head around it too, but the behaviour is correct. 我也花了一些时间来解决这个问题,但是这种行为是正确的。 It is better to look at the list by using the list indexes, rather than a print_r of the internal array. 最好使用列表索引而不是内部数组的print_r查看列表。 This code will do that: 此代码将执行以下操作:

for ($i = 0; $i < $stack->count(); $i++) {
    echo "index $i contains " . $stack->offsetGet($i) . "\n";
}

Note that index 0 represents the top of the list (ie what would be returned by $stack->pop() ). 请注意,索引0代表列表的顶部(即$stack->pop()将返回的内容)。 This is what it looks like after your initial block of code: 这是您的初始代码块之后的样子:

index 0 contains two2
index 1 contains 20152
index 2 contains one2

This is because $stack[] = x is equivalent to $stack->push(x) . 这是因为$stack[] = x等效于$stack->push(x)

Now looking at your next block of code: 现在查看下一个代码块:

$stack->add(0, 'one');

If we look at the manual for add , it says: 如果我们看一下add手册 ,它会说:

Insert the value newval at the specified index, shuffling the previous value at that index (and all subsequent values) up through the list. 在指定的索引处插入值newval,将该索引处的先前值(和所有后续值)重新排列到列表中。

Since the top of the list is index 0, the value in the add has to be inserted at index 1 so that the existing value at index 0 can be "shuffled up through the list" ie it has to remain at index 0. Thus after this code is executed a dump of the list shows this: 由于列表的顶部是索引0,因此必须将添加项中的值插入索引1,以便可以将“索引0”中的现有值“改组为列表”,即必须保留在索引0处。执行此代码后,列表的转储将显示以下内容:

index 0 contains two2
index 1 contains one
index 2 contains 20152
index 3 contains one2

The same thing happens with the other add s, they have to go at the next index down so that the values ahead of them in the list stay that way. 其他add发生相同的事情,它们必须向下一个索引向下移动,以便列表中位于它们前面的值保持该状态。 Thus after the two other adds the list looks like: 因此,在另外两个添加之后,列表如下所示:

index 0 contains two2
index 1 contains one
index 2 contains two
index 3 contains 2015
index 4 contains 20152
index 5 contains one2

And if you try to pop everything off the stack: 如果您尝试将所有内容从堆栈中弹出:

while ($stack->count()) {
    echo $stack->pop() . "\n";
}

You get: 你得到:

two2
one
two
2015
20152
one2

Note : if you want to add a value to the very top of the list you have to use unshift(value) . 注意 :如果要在列表的顶部添加一个值,则必须使用unshift(value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM