Pages

Search This Blog

Tuesday 13 August 2013

Implementation Of Stack Using Array in PHP

<?php
/*
 *
 * @Author: Zuhair Mirza
 * @First Implementation Of Stack Using Array
 * @Date : 30-July-2013
 *
 */

$stack = array();

function push($data, &$stack) {
$stack[] = $data;
}

function pop(&$stack)
{
$len = count($stack);
$top = $stack[$len-1];

unset($stack[$len-1]);

return $top;
}

// array()
print_r($stack);

push(1, $stack);
push(2, $stack);
push('some test', $stack);
push(array(25,12,1999), $stack);

// [1, 2, 'some test', [25, 12, 1999]]
print_r($stack);

// [25, 12, 1999]
echo pop($stack);
// 'some test'
echo pop($stack);

// [1, 2]
print_r($stack);


/*
 * ----------------------THE END-----------------------
 */

?>

No comments:

Post a Comment