La classe SplStack

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

Introduction

La classe SplStack fournit l'interface de base pour implémenter une pile, basée sur une liste doublement chaînée.

Synopsis de la classe

SplStack extends SplDoublyLinkedList implements Iterator , ArrayAccess , Countable {
/* Méthodes */
public __construct()
public setIteratorMode(int $mode): void
/* Méthodes héritées */
public SplDoublyLinkedList::add(mixed $index, mixed $newval): void
public SplDoublyLinkedList::offsetSet(mixed $index, mixed $newval): void
public SplDoublyLinkedList::push(mixed $value): void
public SplDoublyLinkedList::setIteratorMode(int $mode): void
public SplDoublyLinkedList::unserialize(string $serialized): void
public SplDoublyLinkedList::unshift(mixed $value): void
}

Sommaire

add a note add a note

User Contributed Notes 5 notes

up
36
lsroudi at gmail dot com
10 years ago
the SplStack is  simply a SplDoublyLinkedList with  an iteration mode IT_MODE_LIFO and IT_MODE_KEEP
up
9
lincoln dot du dot j at gmail dot com
6 years ago
<?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
up
-22
lsroudi at gmail dot com
10 years ago
<?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
up
-3
daryledesilva at gmail dot com
4 years ago
<?php

// Exercise: Implement Queue using Stacks

class MyQueue {
    protected
$queue;
   
   
/**
     * Initialize your data structure here.
     */
   
function __construct() {
       
$this->queue = new \SplStack;
    }
 
   
/**
     * Push element x to the back of queue.
     * @param Integer $x
     * @return NULL
     */
   
function push($x) {
       
$this->queue->push($x);
    }
 
   
/**
     * Removes the element from in front of queue and returns that element.
     * @return Integer
     */
   
function pop() {
       
$length = count($this->queue);
       
$temp = [];
        while(!
$this->queue->isEmpty()){
           
$rv = $this->queue->pop();
            if(!
$this->queue->isEmpty()){
               
$temp[] = $rv;
            }
        }
        for(
$i = count($temp)-1; $i >= 0; $i--){
           
$this->queue->push($temp[$i]);  
        }
        return
$rv;
    }
 
   
/**
     * Get the front element.
     * @return Integer
     */
   
function peek() {
        return
$this->queue->bottom();
    }
 
   
/**
     * Returns whether the queue is empty.
     * @return Boolean
     */
   
function empty() {
        return
$this->queue->isEmpty();
    }
}

/**
* Your MyQueue object will be instantiated and called as such:
* $obj = MyQueue();
* $obj->push($x);
* $ret_2 = $obj->pop();
* $ret_3 = $obj->peek();
* $ret_4 = $obj->empty();
*/
up
-56
Sandro Alves Peres
10 years ago
<?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

*/
?>
To Top