The SplStack class

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

简介

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

类摘要

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

Table of Contents

User Contributed Notes

daryledesilva at gmail dot com 02-Nov-2019 03:49
<?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();
 */
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 12:03
the SplStack is  simply a SplDoublyLinkedList with  an iteration mode IT_MODE_LIFO and IT_MODE_KEEP
PHP8中文手册 站长在线 整理 版权归PHP文档组所有