Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. <?php
  2.  
  3. class stack {
  4. protected $stack;
  5. protected $limit;
  6.  
  7. public function __construct($limit = 10, $initial = array()) {
  8.  
  9. $this->stack = $initial; // initialize the stack
  10. $this->limit = $limit; // stack can only contain this many items
  11. }
  12.  
  13. public function push($item) {
  14.  
  15. if (count($this->stack) < $this->limit) {
  16.  
  17. array_unshift($this->stack, $item); // prepend item to the start of the array
  18.  
  19. } else {
  20. echo "Stack is full";
  21. }
  22.  
  23. }
  24.  
  25. public function pop() {
  26. if ($this->isEmpty()) {
  27. echo "Stack is empty";
  28. } else {
  29. return array_shift($this->stack); // pop item from the start of the array
  30. }
  31. }
  32.  
  33. public function top() {
  34. return current($this->stack);
  35. }
  36.  
  37. public function isEmpty() {
  38.  
  39. return empty($this->stack);
  40. }
  41.  
  42. }
  43.  
  44.  
  45.  
  46. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement