0

I wrote this implementation of stack and partial implementation of queue using two stacks. Almost everything works as expected besides one thing. For some issue when I var_dump the result of dequeue it returns NULL when when I var_dump the result inside of dequeue it returns a boolean value as expected. Can anyone explain where is the difference coming from?

<?php


class stack
{
    private $stack = array();
    function push($value)
    {
        $this->stack[] = $value;
    }

    function pop()
    {
        if ($this->isEmpty())
            throw new RunTimeException("Stack is empty");   
        $top = $this->stack[count($this->stack)-1];
        unset($this->stack[count($this->stack)-1]);
        $this->stack = array_values($this->stack);
        return $top;
    }
    function isEmpty()
    {
        return empty($this->stack) ? true : false;
    }
    function peak()
    {   
        $top = $this->stack[count($this->stack)];
        return $top;
    }
    function printr()
    {
        print_r($this->stack);
    }
}



class queue
{
    function __construct()
    {
        $this->stack1 = new Stack();
        $this->stack2 = new Stack();
    }

    function push($value)
    {
        $this->stack1->push($value);
    }       

    function dequeue()
    {
        if (!$this->stack2->isEmpty())
        {
            $this->stack2->printr();
            $pop = $this->stack2->pop();
            var_dump($pop);
            return $pop;
        }   
        else if (!$this->stack1->isEmpty())
        {
            do 
            {
                $pop = $this->stack1->pop();
                $this->stack2->push($pop);
            }   
            while ($this->stack1->isEmpty() === false);
            $this->dequeue();
        }
        else
        {
            throw new RunTimeException("Queue is empty");   
        }

    }
    function isEmpty()
    {
        if (($this->stack1->isEmpty()) AND ($this->stack2->isEmpty()))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    function peak()
    {

    }
}


$myQueue = new queue();
$myQueue->push(1);
$myQueue->push(2);
var_dump($myQueue->dequeue());
2
  • ` ? true : false` - why are you doing this? empty already returns a boolean Commented Aug 30, 2015 at 8:11
  • You are completely right. Sorry about that. Commented Aug 31, 2015 at 22:59

2 Answers 2

1

Since you're using recursion, you need to return that value in your second if block.

do { 
   ...
}
while ($this->stack1->isEmpty() === false);
return $this->dequeue();
Sign up to request clarification or add additional context in comments.

Comments

1
do 
{
    $pop = $this->stack1->pop();
    $this->stack2->push($pop);
}   
while ($this->stack1->isEmpty() === false);
$this->dequeue();

The client/calling code will not see the result of $this->dequeue() (above) because, due to recursion, you will be (at least) two levels down (removed) from the client/calling code. This is why you are getting a null result in the client/calling code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.