Help with Stack in PHP

2

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();
}
    
asked by anonymous 18.04.2015 / 02:58

2 answers

1

To access the methods you must use the arrow '- >' and not the '.' Another thing is the __isset method is a method of overloading the isset call to this class, that is when I call isset($classe) it will execute __isset of its class instead of the default. ( more infos here )

Then I took the liberty of renaming your method to _isset (with only an underscore '_') Staying like this:

<?php
class Pilha {

    private $lista = [];
    private $topo = -1;

    public function empilha($valor) {
        $this->topo++;
        $this->lista[$this->topo] = $valor;
    }

    public function remove() {
        if($this->_isset())
            unset($this->lista[$this->topo]);
            $this->topo--;
    } 

    public function _isset() {
        $ret = true;

        if($this->topo < 0) {
            $ret = false;
        }

        return $ret;
    }

    public function getTopo(){
        return $this->lista[$this->topo];
    }
}

//lista principal
$p1 = new Pilha();
//lista de pares
$p2 = new Pilha();
//lista tmp
$p3 = new Pilha();

//preenche pilha de ex. 1,2,3...100
for($i = 1; $i < 101; $i++) {
    $p1->empilha($i);
}

//move pilha
while($p1->_isset()) {
    $topo = $p1->getTopo();

    if($topo % 2 == 0) {
        $p2->empilha($topo);
    }
    else {
        $p3->empilha($topo);
    }

    $p1->remove();
}

//agora p1 esta vazia, p2 esta com os pares e p3 com os impares
//soh jogar o p3 para o p1 ($p1 = $p3) ou fazer um loop (como eh pra facu possivemente o professor vai querer assim)
while($p3->_isset()) {
    $p1->empilha($p3->getTopo());
    $p3->remove();
}

echo "<pre>";
var_dump($p1, $p2, $p3);
    
18.04.2015 / 14:39
0

First version

$arr[1] = [1,2,3,4,5,6,7,8,9,10]; // Pilha 1
$arr[2] = $arr[1]; // Pilha 2

/*
Remove os números ímpares da pilha 1
*/
foreach($arr[1] as $k => $v) if($v%2==0) unset($arr[1][$k]);

/*
Remove os números paresda pilha 2
*/
foreach($arr[2] as $k => $v) if($v%2!=0) unset($arr[2][$k]);

/*
Imprime os resultados (finalidade de testes)
*/
print_r($arr[1]);

/*
Necessário verificar se a pilha 2 existe e não está vazia.
*/
if (isset($arr[2]) && !empty($arr[2]))
    print_r($arr[2]);
  • Exercise does not talk about class usage or object orientation.
  • Exercise does not require the use of "stack 3".
  • The exercise does not specify that it is not allowed to "clone" all data from stack 1 to stack 2.
  • The result returns what is requested.
  • Second version, optimized

    /*
    Pilha 1
    */
    $arr[1] = [1,2,3,4,5,6,7,8,9,10];
    $arr[2] = [];
    
    foreach( $arr[1] as $k => $v )
    {
        if($v%2==0)
        {
            /*
            Atribui os valores pares a pilha 2
            */
            $arr[2][] = $v;
            /*
            Remove os valores pares da pilha 1
            */
            unset($arr[1][$k]);
        }
    }
    
    /*
    Imprime os resultados (finalidade de testes)
    */
    print_r($arr[1]);
    
    /*
    Necessário verificar se a pilha 2 existe e não está vazia.
    */
    if (isset($arr[2]) && !empty($arr[2]))
        print_r($arr[2]);
    

    In this second version, the second stack receives only the odd values. unlike the first version where the second stack receives all the data from stack 1 to then follow the filtering procedures.

        
    02.08.2015 / 15:06