<?php
class Stack {
private $stack_array = array();
private $index = -1;
public function print_array()
{
print_r($this->stack_array);
}
public function isEmpty()
{
if($this->index == -1){
return true;
}else{
return false;
}
}
public function push($element)
{
$this->index++;
$this->stack_array[$this->index] = $element;
}
public function pop()
{
if($this->isEmpty()){
echo 'The array doesn\'t contain any elemnet';
}else{
$element = $this->stack_array[$this->index];
unset($this->stack_array[$this->index]);
$this->index--;
}
}
public function pop_from_last()
{
if($this->isEmpty()){
echo 'The array doesn\'t contain any elemnet';
}else{
$element = $this->stack_array[0];
unset($this->stack_array[0]);
$this->index--;
return $element;
}
}
public function first()
{
if($this->isEmpty()){
echo 'The array doesn\'t contain any elemnet';
}else{
return $this->stack_array[$this->index];
}
}
public function last()
{
if($this->isEmpty()){
echo 'The array doesn\'t contain any elemnet';
}else{
return $this->stack_array[0];
}
}
public function get_element($position)
{
if($position >= $this->index + 1){
echo 'position is exceeding the array index';
}else{
$i = $this->index - ($position - 1);
return $this->stack_array[$i];
}
}
}
$stack = new Stack();
$stack->push(8);
$stack->push(5);
$stack->push(4);
$stack->push(9);
$stack->push(2);
$stack->print_array();
echo '<br>';
echo $stack->get_element(1);
echo '<br>';
echo $stack->get_element(2);
echo '<br>';
echo $stack->get_element(3);
echo '<br>';
echo $stack->get_element(30);
class Stack {
private $stack_array = array();
private $index = -1;
public function print_array()
{
print_r($this->stack_array);
}
public function isEmpty()
{
if($this->index == -1){
return true;
}else{
return false;
}
}
public function push($element)
{
$this->index++;
$this->stack_array[$this->index] = $element;
}
public function pop()
{
if($this->isEmpty()){
echo 'The array doesn\'t contain any elemnet';
}else{
$element = $this->stack_array[$this->index];
unset($this->stack_array[$this->index]);
$this->index--;
}
}
public function pop_from_last()
{
if($this->isEmpty()){
echo 'The array doesn\'t contain any elemnet';
}else{
$element = $this->stack_array[0];
unset($this->stack_array[0]);
$this->index--;
return $element;
}
}
public function first()
{
if($this->isEmpty()){
echo 'The array doesn\'t contain any elemnet';
}else{
return $this->stack_array[$this->index];
}
}
public function last()
{
if($this->isEmpty()){
echo 'The array doesn\'t contain any elemnet';
}else{
return $this->stack_array[0];
}
}
public function get_element($position)
{
if($position >= $this->index + 1){
echo 'position is exceeding the array index';
}else{
$i = $this->index - ($position - 1);
return $this->stack_array[$i];
}
}
}
$stack = new Stack();
$stack->push(8);
$stack->push(5);
$stack->push(4);
$stack->push(9);
$stack->push(2);
$stack->print_array();
echo '<br>';
echo $stack->get_element(1);
echo '<br>';
echo $stack->get_element(2);
echo '<br>';
echo $stack->get_element(3);
echo '<br>';
echo $stack->get_element(30);
No comments:
Post a Comment