PHP – Stack and Queue

June 16, 2009 by: Allen Sanford

I have been ask on more than one occasion by beginners in PHP, especially those fresh out of college or those still in college, how would I do a stack and queue structure in PHP. Well the truth is that PHP has built in functions to manipulate arrays as though the were either a Stack or a Queue, so it is basically something that is done all the time without any thought. So you maybe asking why am I writing a tutorial, well just to humor those who wish to be humored I guess.

Well I guess I will start with the Stack. For this exercise I am going to create a class called Stack and then I will implement the following methods: push, pop, and I don’t know display maybe and for good measure we will make it a vertical stack when we display it. This script will of coarse will be written to run in the browser by the way.

?Download stack.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
    class Stack
    {
        var $stack = array();
        function __construct() {  }
        function push($var) { return array_push($this->stack, $var); }
        function pop() { return array_pop($this->stack); }
        function display($arr=null)
        {
            if (!is_null($arr))
                $temp = $arr;
            else
                $temp = array_reverse($this->stack);
 
            foreach ($temp as $val)
            {
                 if (is_array($var))
                     $this->display($var);
                 else
                     echo $var . "<br>";
            }
        }
    }
?>

Now that is what a simple Stack class looks like in PHP easy uh. The hardest thing was trying to print out the Stack, funny huh. Well now we get to do the queue which by the way is stupid easy as well. Again we are going to implement the simple and only the most basic methods.

?Download queue.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
    class Queue
    {
        var $queue = array();
        function __construct() {  }
        function push($var) { return array_push($this->queue, $var); }
        function pop() { return array_shift($this->queue); }
        function display($arr=null)
        {
            if (!is_null($arr))
                $temp = $arr;
            else
                $temp = $this->queue;
 
            foreach ($temp as $val)
            {
                 if (is_array($var))
                     $this->display($var);
                 else
                     echo $var . " ";
            }
        }
    }
?>

Pretty stupid ain’t it. The only thing that changes is the reverse_array call. Any way I wrote the sticking tutorial even thought you don’t really need it because if you look closely you see there are function built into PHP called array_push(), array_pop(), and for the queue array_shift which are used in this code. All we did was write a wrapper to go around those functions and write a very simply display function, which by the way will break it you make the stack or queue a stack or queue of objects.

Anyway, Have a Good’n!

Filed under: PHP
Tags: , , ,

Comments

6 Responses to “PHP – Stack and Queue”
  1. Kurt Krueckeberg says:

    Not to nitpick, but your Queue::pop() and Stack::pop() don’t return anything.

  2. R. Sack says:

    0){
    foreach(self::$queue as $item){
    echo ++$count .’: ‘. $item . ”;
    }
    } else {
    echo ‘I\’m empty’.”;
    }

    echo ‘———————————-’.”;
    }
    }

    Queue::push(‘goose’);
    Queue::push(‘chicken’);
    Queue::push(‘duck’);
    Queue::push(‘albatross’);
    Queue::pop();
    Queue::pop();
    Queue::pop();
    Queue::pop();
    ?>

  3. R. Sack says:

    Queue fixed.

    0){
    foreach(self::$queue as $item){
    echo ++$count .’: ‘. $item . ”;
    }
    } else {
    echo ‘I\’m empty’.”;
    }

    echo ‘———————————-’.”;
    }
    }

    Queue::push(‘goose’);
    Queue::push(‘chicken’);
    Queue::push(‘duck’);
    Queue::push(‘albatross’);
    Queue::pop();
    Queue::pop();
    Queue::pop();
    Queue::pop();
    ?>

  4. R. Sack says:

    Sorry. This script keeps chopping off my fixed queue code. How can I submit it to you?

  5. Peter says:

    Those people who really care about that should ask themself if it’s the right work for them.

Leave a Reply