2
I have a question in a type of structure in PHP. More precisely in an exercise.
Write a program so that given three P1, P2 and P3 batteries.
The P1 stack has any n integer numbers. P2 and P3 stacks are empty. You want to pass all even numbers from the P1 stack to the P2 stack. Stack P3, if necessary, can be used as an auxiliary stack. At the end P1 will have odd numbers, P2 even numbers and P3 empty.
I cannot use ready-made function as array_reserve for example and the like. I tried to do it this way, 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();
}
Needs to be sorted each stack?
– Skywalker