<?php
class stack {
var $arrays;
var $size;
function stack() { // class constructor
$this->size = 0;
unset($this->arrays);
}
function push($elem) { // put an element on stack
$this->arrays[$this->size] = $elem;
$this->size++;
}
function get_size() { // get number of elements stored
return $this->size;
}
function is_empty() { // is stack empty ?
return ($this->size == 0) ? true : false;
}
function pop() { // retrieve an element from the top of stack
if ( $this->is_empty() == false ) {
$this->size--;
return $this->arrays[$this->size];
}
else
return 0;
}
}
$inst = new stack; // create an object from stack class
echo "initial stack size=".($inst->get_size()),"<BR>\n";
for ($i=0; $i < 10; $i++) {
$inst->push( ($i*7)%11 );
}
echo "current stack size=".($inst->get_size()),"<BR>\n";
while (! $inst->is_empty() ) {
echo "pop ".$inst->pop(),"<BR>\n";
}
echo "stack is ".($inst->is_empty() ? "empty." : "not empty.")."<BR>\n";
$inst = 0; // unuse this instance of class stack
?>