Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2012
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.62 KB | None | 0 0
  1. <?php
  2. class element{
  3.     var $val;
  4.     var $next;
  5. }
  6.  
  7. class stack{
  8.     var $current = null;
  9.    
  10.     function push($val){
  11.         $element = new element();
  12.         $element->val = $val;
  13.         $element->next = $this->current;
  14.         $this->current = $element;
  15.     }
  16.    
  17.     function pop(){
  18.         if($this->current == null) return -1;
  19.         $tmp = $this->current;
  20.         $this->current = $tmp->next;
  21.         return $tmp->val . "\n";
  22.     }
  23. }
  24.  
  25. $stack = new stack();
  26.  
  27. $stack->push(5);
  28. $stack->push(4);
  29. $stack->push(2);
  30.  
  31. echo $stack->pop();
  32. echo $stack->pop();
  33. echo $stack->pop();
  34. echo $stack->pop();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement