>  기사  >  백엔드 개발  >  PHP 데이터 구조의 연결 목록 대기열 예제에 대한 자세한 설명

PHP 데이터 구조의 연결 목록 대기열 예제에 대한 자세한 설명

黄舟
黄舟원래의
2017-10-18 09:11:59985검색

이 글은 주로 PHP 데이터 구조 연결 목록 큐 관련 정보를 소개합니다. 도움이 필요한 친구들은

php 연결 목록 큐

예제 코드:


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; 
    if(empty($this->first)){ 
      $this->first = $this->last; 
    }else{ 
      $this->oldfirst->next = $this->last; 
    } 
    self::$n++; 
  } 
   
  public function pop(){ 
    if(self::$n<0){ 
      return null; 
    } 
    $item = $this->first->item; 
    $this->first = $this->first->next; 
    self::$n--; 
    return $item; 
  } 
   
} 
 
class Node{ 
  public $item; 
  public $next; 
} 
 
$Queue = new Queue(); 
$Queue->push("a"); 
$Queue->push("b"); 
$Queue->push("c"); 
echo $Queue->pop().PHP_EOL; 
echo $Queue->pop().PHP_EOL; 
echo $Queue->pop().PHP_EOL; 
echo $Queue->pop().PHP_EOL;
를 참조하세요.

위 내용은 PHP 데이터 구조의 연결 목록 대기열 예제에 대한 자세한 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.