<?php class Stack { public $maxSize; public $items = []; public $top = 0; public function __construct(array $stack, $maxSize){ $this->items = $stack; $this->maxSize = $maxSize; } public function push($item){ if ($this->top === $this->maxSize){ throw new Exception("Stack overflow"); } $this->items[$this->top] = $item; $this->top++; } public function pop(){ if ($this->top === 0){ throw new Exception("Stack underflow"); } $this->top--; return $this->items[$this->top]; } } $items = [1, 2, 3, 4, 5]; $maxSize = 10; $stack = new Stack($items, $maxSize); $stack->push(6); var_dump($stack); $stack->pop(); var_dump($stack);
You have javascript disabled. You will not be able to edit any code.