文字

The SplStack class

(PHP 5 >= 5.3.0)

简介

The SplStack class provides the main functionalities of a stack implemented using a doubly linked list.

类摘要

SplStack extends SplDoublyLinkedList implements Iterator , ArrayAccess , Countable {
__construct ( void )
void setIteratorMode ( int $mode )
public void SplDoublyLinkedList::add ( mixed $index , mixed $newval )
public mixed SplDoublyLinkedList::bottom ( void )
public int SplDoublyLinkedList::count ( void )
public mixed SplDoublyLinkedList::current ( void )
public int SplDoublyLinkedList::getIteratorMode ( void )
public bool SplDoublyLinkedList::isEmpty ( void )
public mixed SplDoublyLinkedList::key ( void )
public void SplDoublyLinkedList::next ( void )
public bool SplDoublyLinkedList::offsetExists ( mixed $index )
public mixed SplDoublyLinkedList::offsetGet ( mixed $index )
public void SplDoublyLinkedList::offsetSet ( mixed $index , mixed $newval )
public void SplDoublyLinkedList::offsetUnset ( mixed $index )
public mixed SplDoublyLinkedList::pop ( void )
public void SplDoublyLinkedList::prev ( void )
public void SplDoublyLinkedList::push ( mixed $value )
public void SplDoublyLinkedList::rewind ( void )
public string SplDoublyLinkedList::serialize ( void )
public void SplDoublyLinkedList::setIteratorMode ( int $mode )
public mixed SplDoublyLinkedList::shift ( void )
public mixed SplDoublyLinkedList::top ( void )
public void SplDoublyLinkedList::unserialize ( string $serialized )
public void SplDoublyLinkedList::unshift ( mixed $value )
public bool SplDoublyLinkedList::valid ( void )
}

Table of Contents

  • SplStack::__construct — Constructs a new stack implemented using a doubly linked list
  • SplStack::setIteratorMode — Sets the mode of iteration

用户评论:

[#1] lsroudi at gmail dot com [2014-01-19 20:07:37]

<?php


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();
        }
    }

    

    
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

[#2] lsroudi at gmail dot com [2014-01-19 00:03:13]

the SplStack is  simply a SplDoublyLinkedList with  an iteration mode IT_MODE_LIFO and IT_MODE_KEEP

[#3] Sandro Alves Peres [2013-06-12 12:04:13]

<?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();
}


?>

上一篇: 下一篇: