php教程

The SplStack class

(PHP 5 >= 5.3.0, PHP 7)

简介

SplStack类通过使用一个双向链表来提供栈的主要功能。

类摘要

SplStack extends SplDoublyLinkedList implements Iterator , ArrayAccess , Countable {
/* 方法 */
__construct ( void )
setIteratorMode ( int $mode ) : void
/* 继承的方法 */
public SplDoublyLinkedList::add ( mixed $index , mixed $newval ) : void
public SplDoublyLinkedList::count ( void ) : int
public SplDoublyLinkedList::isEmpty ( void ) : bool
public SplDoublyLinkedList::key ( void ) : mixed
public SplDoublyLinkedList::next ( void ) : void
public SplDoublyLinkedList::offsetExists ( mixed $index ) : bool
public SplDoublyLinkedList::offsetSet ( mixed $index , mixed $newval ) : void
public SplDoublyLinkedList::offsetUnset ( mixed $index ) : void
public SplDoublyLinkedList::pop ( void ) : mixed
public SplDoublyLinkedList::prev ( void ) : void
public SplDoublyLinkedList::push ( mixed $value ) : void
public SplDoublyLinkedList::rewind ( void ) : void
public SplDoublyLinkedList::serialize ( void ) : string
public SplDoublyLinkedList::setIteratorMode ( int $mode ) : void
public SplDoublyLinkedList::top ( void ) : mixed
public SplDoublyLinkedList::unserialize ( string $serialized ) : void
public SplDoublyLinkedList::unshift ( mixed $value ) : void
public SplDoublyLinkedList::valid ( void ) : bool
}

Table of Contents

User Contributed Notes

lincoln dot du dot j at gmail dot com 12-Jul-2017 06:48
<?php
//SplStack Mode is LIFO (Last In First Out)
 
$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
lsroudi at gmail dot com 19-Jan-2014 08:07
<?php

/**
 * Description of Stack
 *
 * (c) lsroudi <http://lsroudi.com/> <lsroudi@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
class Stack {

    private
$splstack;

    function
__construct(\SplStack $splstack)
    {
       
$this->splstack = $splstack;
    }

    public function
calculateSomme()
    {

        if (
$this->splstack->count() > 1){
           
$val1 = $this->splstack->pop();
           
$val2 = $this->splstack->pop();
           
$val = $val1 + $val2;
           
$this->splstack->push($val);
           
$this->calculateSomme();
        }
    }

   
/**
     *
     * @return integer
     */
   
public function displaySomme()
    {
       
$result = $this->splstack->pop();
        return
$result;
    }

}

$splstack = new \SplStack();

$splstack->push(10);
$splstack->push(10);
$splstack->push(10);
$splstack->push(10);
$splstack->push(10);
$splstack->push(10);
$splstack->push(10);
$splstack->push(10);
$splstack->push(10);
$splstack->push(10);
$splstack->push(10);
$splstack->push(10);
$splstack->push(10);
$splstack->push(10);
$splstack->push(10);

$stack = new Stack($splstack);
$stack->calculateSomme();
die(
var_dump($stack->displaySomme())); // 150
?>
source : https://github.com/lsroudi/OOPWithSPL/blob/master/Stack/Stack.php
lsroudi at gmail dot com 19-Jan-2014 12:03
the SplStack is  simply a SplDoublyLinkedList with  an iteration mode IT_MODE_LIFO and IT_MODE_KEEP
Sandro Alves Peres 12-Jun-2013 12:04
<?php
# Think of the stack as an array reversed, where the last element has index zero

$stack = new SplStack();
$stack->push('a');
$stack->push('b');
$stack->push('c');

$stack->offsetSet(0, 'C'); # the last element has index zero

$stack->rewind();

while(
$stack->valid() )
{
    echo
$stack->current(), PHP_EOL;
   
$stack->next();
}

/*

OUTPUT
****************************

C
b
a

*/
?>

CopyRight © 2008-2022 verySource.Com All Rights reserved. 京ICP备17048824号-1 京公网安备:11010502034788