Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
21
Task/Queue-Definition/PHP/queue-definition-1.php
Normal file
21
Task/Queue-Definition/PHP/queue-definition-1.php
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
class Fifo {
|
||||
private $data = array();
|
||||
public function push($element){
|
||||
array_push($this->data, $element);
|
||||
}
|
||||
public function pop(){
|
||||
if ($this->isEmpty()){
|
||||
throw new Exception('Attempt to pop from an empty queue');
|
||||
}
|
||||
return array_shift($this->data);
|
||||
}
|
||||
|
||||
//Alias functions
|
||||
public function enqueue($element) { $this->push($element); }
|
||||
public function dequeue() { return $this->pop(); }
|
||||
|
||||
//Note: PHP prevents a method name of 'empty'
|
||||
public function isEmpty(){
|
||||
return empty($this->data);
|
||||
}
|
||||
}
|
||||
9
Task/Queue-Definition/PHP/queue-definition-2.php
Normal file
9
Task/Queue-Definition/PHP/queue-definition-2.php
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
$foo = new Fifo();
|
||||
$foo->push('One');
|
||||
$foo->enqueue('Two');
|
||||
$foo->push('Three');
|
||||
|
||||
echo $foo->pop(); //Prints 'One'
|
||||
echo $foo->dequeue(); //Prints 'Two'
|
||||
echo $foo->pop(); //Prints 'Three'
|
||||
echo $foo->pop(); //Throws an exception
|
||||
Loading…
Add table
Add a link
Reference in a new issue