0

I'm trying to write a method for a class Queue that inverts the whole Queue. After running the program, it gives the follow problem:

Cannot use object of type Queue as array on line echo($i.". ".$this->kolejka[$i-1]."<br>");

Apparently when he is trying to use printOut method again on the inverted Queue. Please help!

Please don't laugh (too hard) as I tried many things to make this work and I'm lost.

Here is the whole code:

<?php
class Queue
{
private $Queue = array(); //Init

public function clear() //Clears the Queue
{
    $this->Queue = array();
}


public function isMember($item) //Returns True if element is in the Queue
{
    foreach($this->Queue as $x)
    {
        if($item === $x)
        {

            return true;
        }
    }
    return false;
}


public function remove() //Removes first element
{
    return array_shift($this->Queue);
}


public function add($item) //Adds element to the end
{
    $this->Queue[] = $item;
}


public function first() //Returns the first element
{
    return current($this->Queue);
}

public function printOut() //Writes down in order all the elements
{
  for($i=1;$i < count($this->Queue)+1;$i++)
  {
    echo($i.". ".$this->Queue[$i-1]."<br>");
  }

} 

    public function length() //Returnts length
{
  return count($this->Queue);
}

public function invert() //Reverts the Queue
{
   $newQueue = new Queue();
   for ($i = $this->length() - 1;$i>=0;$i--)
   {
     $newQueue->add($this->first());
     $this->remove();
   }
   $this->Queue = $newQueue;
}

}

$kolej = new Queue();
$kolej->add("Apple");
$kolej->add("Orange");
$kolej->add("Banana");
$kolej->add("Mandarin");
$kolej->add("Raspberry");
echo $kolej->first()."<br>";
$kolej->remove();
echo $kolej->first()."<br>";
echo $kolej->isMember("Apple")."<br>";
echo $kolej->isMember("Orange")."<br>";
$kolej->printOut();
echo "Currently Queue is of length ".$kolej->length()."<br>";
$kolej->invert();
$kolej->printOut();
?>
Leigh
  • 28,765
  • 10
  • 55
  • 103
Eric Luther
  • 117
  • 1
  • 10

2 Answers2

0

your invert() function is doing the wrong thing. $this->Queue is supposed to be an array:

private $Queue = array(); //Init

, but at the end of the function you are setting it to an object (named $newQueue):

public function invert() //Reverts the Queue
{
   $newQueue = new Queue();
   for ($i = $this->length() - 1;$i>=0;$i--)
   {
     $newQueue->add($this->first());
     $this->remove();
   }
   $this->Queue = $newQueue;
}

You can solve this in one of two ways:

  1. set $this->Queue to $newQueue->Queue (you probably have to make it a non-private variable)
  2. learn how to invert an array in place instead of creating a temporary array
Community
  • 1
  • 1
Dan O
  • 6,022
  • 2
  • 32
  • 50
  • Thanks very much, now I see it. Apart from that I saw another important issue with the algorithm. The method does not revert it but copies it exactly. Now it's fixed. Thanks again :-) – Eric Luther Jun 11 '13 at 21:38
  • you're welcome! In the future, if you find an answer to be useful then you can vote for it by clicking the up arrow next to it, or you can click the little checkbox to mark it as the accepted answer to your question. – Dan O Jun 12 '13 at 01:11
0

Here is solid solution for the invert method. I hope this helps.

//Reverts the Queue
public function invert() { 
    $newQueue = array();
    for ($i = 0; $i < count($this->Queue) + 1; $i++) {
        $newQueue[$i] = $this->Queue[count($this->Queue) - 1 - $i];
        echo $newQueue[$i] . '<br/>';
    }
}
Magellan
  • 1,224
  • 9
  • 16