note 124361 added to class.splstack
| From: | daryledesilva at gmail dot com | Date: | Sat, 02 Nov 2019 15:50:00 +0000 |
| Subject: | note 124361 added to class.splstack | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-212882@lists.php.net to get a copy of this message | ||
<?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();
*/
----
Server IP: 208.43.231.9
Probable Submitter: 185.85.3.195 (proxied: 219.75.80.23)
----
Manual Page -- http://php.net/manual/en/class.splstack.php
Edit -- https://master.php.net/note/edit/124361
Del: integrated -- https://master.php.net/note/delete/124361/integrated
Del: useless -- https://master.php.net/note/delete/124361/useless
Del: bad code -- https://master.php.net/note/delete/124361/bad+code
Del: spam -- https://master.php.net/note/delete/124361/spam
Del: non-english -- https://master.php.net/note/delete/124361/non-english
Del: in docs -- https://master.php.net/note/delete/124361/in+docs
Del: other reasons-- https://master.php.net/note/delete/124361
Reject -- https://master.php.net/note/reject/124361
Search -- https://master.php.net/manage/user-notes.php