简体   繁体   English

Splstack 它是如何工作的? 为什么?

[英]Splstack how it's works? and why?

<?php
    $stack = new SplStack();

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

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

When i run this code, all items are stored an property called dllist (i hope) and it's the expected result.当我运行此代码时,所有项目都存储了一个名为dllist的属性(我希望),这是预期的结果。

SplStack Object
(
    [flags:SplDoublyLinkedList:private] => 6
    [dllist:SplDoublyLinkedList:private] => Array
        (
            [0] => one
            [1] => two
            [2] => 2015
        )

)

But i run the code below, works the same way that code above, why when i try, $stack[] = 'one';但是我运行下面的代码,工作方式与上面的代码相同,为什么当我尝试时, $stack[] = 'one'; why does not get an error?为什么没有得到错误? how php know that $stack[] = 'one'; php 怎么知道$stack[] = 'one'; should be stored in dllist ?应该存储在dllist吗?

<?php
    $stack = new SplStack();

    $stack[] = 'one';
    $stack[] = 2015;
    $stack[] = 'two';

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

I expected some errors like this statements:我预计会出现类似以下语句的错误:

<?php

    $int = 2015;
    $int[] = 300; //Warning: Cannot use a scalar value as an array in 

    $str = 'foo';
    $str[] = 'foo'; //Fatal error: [] operator not supported for strings in

    $obj = new stdClass(); 
    $obj[] = 'new one'; //Fatal error: Cannot use object of type stdClass as array in 

SplStack 实现了接口 ArrayAccess,使得 SplStack 类具有数组特性: http ://php.net/manual/en/class.arrayaccess.php

Stack: In the pushdown stacks only two operations are allowed: push the item into the stack, and pop the item out of the stack.堆栈:在下推堆栈中只允许两种操作:将项目推入堆栈,并将项目弹出堆栈。 A stack is a limited access data structure - elements can be added and removed from the stack only at the top.堆栈是一种访问受限的数据结构 - 元素只能在堆栈顶部添加和删除。 push adds an item to the top of the stack, pop removes the item from the top. push 将一个项目添加到堆栈的顶部,pop 从顶部删除该项目。 A helpful analogy is to think of a stack of books;一个有用的比喻是想想一堆书; you can remove only the top book, also you can add a new book on the top.您可以只删除顶部的书,也可以在顶部添加新书。

堆

Queue: An excellent example of a queue is a line of students in the food court of the UC.排队:排队的一个很好的例子是加州大学美食广场的一排学生。 New additions to a line made to the back of the queue, while removal (or serving) happens in the front.在队列后面添加新的一行,而删除(或服务)发生在前面。 In the queue only two operations are allowed enqueue and dequeue.在队列中只允许入队和出队两个操作。 Enqueue means to insert an item into the back of the queue, dequeue means removing the front item. Enqueue 意味着将一个 item 插入到队列的后面,dequeue 意味着移除前面的 item。 The picture demonstrates the FIFO access.该图演示了 FIFO 访问。 The difference between stacks and queues is in removing.堆栈和队列之间的区别在于删除。 In a stack we remove the item the most recently added;在堆栈中,我们删除最近添加的项目; in a queue, we remove the item the least recently added.!在队列中,我们删除最近最少添加的项目。!

For example stack例如堆栈

Last push element on top顶部的最后一个推送元素

last-in first-out (LIFO)后进先出 (LIFO)


$q = new SplStack();

$q[] = 1;
$q[] = 2;
$q[] = 3;
$q->push(4);
$q->add(4,5);

$q->rewind();
while($q->valid()){
    echo $q->current(),"\n";
    $q->next();
}
?>

Output
5
4
3
2
1

For example queue例如队列

Last push element in bottom底部的最后一个推送元素

first-in first-out (FIFO)先进先出 (FIFO)


<?php 


// Create a new empty queue 

$gfg = new SplQueue(); 



$gfg[] = 1; 

$gfg[] = 2; 

$gfg[] = 3; 


// Dequeue element 

echo $gfg->dequeue() . "\n"; 

echo $gfg->dequeue() . "\n"; 


?> 
Output:
1
2

Be careful someone asc someone desc小心有人升有人降

如何工作堆栈另一个例子

队列是如何工作的

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

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