Advertisement
tsuedeun

Untitled

Jun 8th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.99 KB | None | 0 0
  1. <?php
  2. class Queue {
  3.     public $first, $last;
  4.  
  5.     public function __construct() {
  6.         $this->last = null;
  7.         $this->first = null;
  8.     }
  9.  
  10.     public function getList() {
  11.         $curr = $this->last;
  12.         while ($curr != null) {
  13.             yield $curr->data;
  14.             $curr = $curr->next;
  15.         }
  16.     }
  17.  
  18.     public function contains($node) {
  19.         foreach ($this->getList() as $curr)
  20.             if ($curr == $node)
  21.                 return true;
  22.         return false;
  23.     }
  24.  
  25.     public function put($value) {
  26.         $node = new Node($value);
  27.         if ($this->first == null) {
  28.             $this->first = $node;
  29.             $this->last = $node;
  30.         } else {
  31.             $this->last->next = $node;
  32.             $this->last = $node;
  33.         }
  34.     }
  35.  
  36.     public function get() {
  37.         if ($this->first == null) return 'Очередь пуста';
  38.         $item = $this->first->data;
  39.         $this->first = $this->first->next;
  40.         return $item;
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement