Linked list queue of php data structure

  • 2021-08-10 06:53:20
  • OfStack

php linked list queue

Example code:

class Queue{ 
  private $last; 
  private $first; 
  private $oldfirst; 
  private static $n=0; 
  public function __construct(){ 
    $this->last   = null; 
    $this->first  = null; 
    $this->oldfirst = null; 
  public function push($item){ 
    $this->oldfirst = $this->last; 
    $this->last = new Node(); 
    $this->last->item = $item; 
    $this->last->next = null; 
      $this->first = $this->last; 
      $this->oldfirst->next = $this->last; 
  public function pop(){ 
      return null; 
    $item = $this->first->item; 
    $this->first = $this->first->next; 
    return $item; 
class Node{ 
  public $item; 
  public $next; 
$Queue = new Queue(); 
echo $Queue->pop().PHP_EOL; 
echo $Queue->pop().PHP_EOL; 
echo $Queue->pop().PHP_EOL; 
echo $Queue->pop().PHP_EOL;

If you have any questions, please leave a message or go to this site community to exchange and discuss, thank you for reading, hope to help everyone, thank you for your support to this site!

Related articles: