Help with PHP Stack

Asked

Viewed 860 times

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?

2 answers

1

To access the methods you must use the arrow '->' and not the point '.'. Another thing is the method __isset it is a method of isset call overloading for that class, ie when I call isset($classe) he will execute the __isset of your class instead of the standard. (more hints here)

So I took the liberty of renaming your method to _isset (with only one underscore '_') Thus remaining:

<?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);
  • I would keep the magic method __isset and would change the calls at while to isset($p1)

  • o remove should not return the top?

  • Unnecessary to the class Pilha. In php we already have a guy named SplStack and SplDoubleLinkedList.

  • 1

    If you read the question is an exercise, ie this one (was) learning the concepts of stack.

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]);
  1. Exercise does not talk about use of classes or object orientation.
  2. Exercise does not require the use of "stack 3".
  3. Exercise does not specify that it is not allowed to "clone" all data from stack 1 to stack 2.
  4. The result returns what is requested.

Second version, optimized for

/*
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 stick receives only odd values. unlike the first version where the second stack receives all data from stack 1 to then follow the filtering procedures.

  • 1

    This is not stack, this is normal array.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.