The SplStack class

(PHP 5 >= 5.3.0)

Introduction

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

Class synopsis

SplStack extends SplDoublyLinkedList implements Iterator , ArrayAccess , Countable {
/* Methods */
__construct ( void )
void setIteratorMode ( int $mode )
/* Inherited methods */
public void SplDoublyLinkedList::add ( mixed $index , mixed $newval )
public mixed SplDoublyLinkedList::bottom ( void )
public int SplDoublyLinkedList::count ( void )
public mixed SplDoublyLinkedList::current ( void )
public bool SplDoublyLinkedList::isEmpty ( void )
public mixed SplDoublyLinkedList::key ( void )
public void SplDoublyLinkedList::next ( void )
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

Коментарии

the SplStack is  simply a SplDoublyLinkedList with  an iteration mode IT_MODE_LIFO and IT_MODE_KEEP
2014-01-19 02:03:13
http://php5.kiev.ua/manual/ru/class.splstack.html
<?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
2017-07-12 09:48:20
http://php5.kiev.ua/manual/ru/class.splstack.html
<?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();
 */
2019-11-02 17:49:59
http://php5.kiev.ua/manual/ru/class.splstack.html

    Поддержать сайт на родительском проекте КГБ