I am having a question about a structure type in PHP. More precisely in an exercise.
Write a program to give you three P1, P2, and P3 batteries.
The stack P1 has n any integers. The batteries P2 and P3 are empty. It is desired to pass all even numbers of the stack P1 to the stack P2. The P3 battery, if necessary, can be used as an auxiliary battery. At the end P1 will have the odd numbers, P2 the even numbers and P3 will empty.
I can not use function ready like array_reserve for example and the like. I tried to do that, but it's making a mistake. Would anyone know why?
<?php
class Pilha {
private $lista;
public $valor = 20;
public $topo = -1;
public function empilha($valor)
{
$this->lista[] = $valor;
$this->topo++;
}
public function remove()
{
$this->topo--;
}
public function __isset($lista)
{
if ($this->topo < 0) {
return true;
} else {
return false;
}
}
public function getTopo()
{
return $this->lista[$this->topo];
}
}
$p1 = new Pilha();
$p2 = new Pilha();
$p3 = new Pilha();
while (!$p1 . __isset($lista)) {
if ($p1 . getTopo() % 2 == 0) {
$p3 . empilha($p1 . getTopo);
}
$p1 . remove();
}
while (!$p3 . __isset($lista)) {
$p2 . empilha($p3 . getTopo);
$p3 . remove();
}