>  기사  >  백엔드 개발  >  PHP는 연결된 목록을 생성하고 연결된 목록 노드를 추가, 삭제, 업데이트 및 순회합니다.

PHP는 연결된 목록을 생성하고 연결된 목록 노드를 추가, 삭제, 업데이트 및 순회합니다.

墨辰丷
墨辰丷원래의
2018-06-08 17:59:423157검색

이 글은 주로 PHP에서 연결리스트를 생성하는 방법과 연결리스트 노드를 추가, 삭제, 업데이트, 순회하는 방법을 소개합니다. 관심 있는 친구들이 참고하면 도움이 될 것입니다.

이 문서의 예제에서는 다음과 같이 PHP 연결 목록의 사용법을 설명합니다.

다음은 연결 목록 노드의 생성, 순회 및 업데이트를 포함하여 PHP 연결 목록의 기본 사용법에 대한 간략한 소개입니다.

<?php
/**
 * @author MzXy
 * @copyright 2011
 * @param PHP链表
 */
/**
*
*节点类
*/
class Node
{
  private $Data;//节点数据
  private $Next;//下一节点
  public function setData($value){
    $this->Data=$value;
  }
  public function setNext($value){
     $this->Next=$value;
  }  
  public function getData(){
    return $this->Data;
  }
  public function getNext(){
    return $this->Next;
  }
  public function __construct($data,$next){
    $this->setData($data);
    $this->setNext($next);
  }
}//功能类
class LinkList
{
  private $header;//头节点
  private $size;//长度
  public function getSize(){
    $i=0;
    $node=$this->header;
    while($node->getNext()!=null)
    {  $i++;
      $node=$node->getNext();
    }
   return $i;
  }
  public function setHeader($value){
    $this->header=$value;
  }
  public function getHeader(){
    return $this->header;
  }
  public function __construct(){
     header("content-type:text/html; charset=utf-8");
    $this->setHeader(new Node(null,null));
  }
  /**
  *@author MzXy
  *@param $data--要添加节点的数据
  * 
  */
  public function add($data)
  {
    $node=$this->header;
    while($node->getNext()!=null)
    {
      $node=$node->getNext();
    }
    $node->setNext(new Node($data,null));
  }
   /**
  *@author MzXy
  *@param $data--要移除节点的数据
  * 
  */
  public function removeAt($data)
  {
    $node=$this->header;
    while($node->getData()!=$data)
    {
      $node=$node->getNext();
    }
    $node->setNext($node->getNext());
    $node->setData($node->getNext()->getData());
  }
   /**
  *@author MzXy
  *@param 遍历
  * 
  */
  public function get()
  {
    $node=$this->header;
    if($node->getNext()==null){
      print("数据集为空!");
      return;
    }
    while($node->getNext()!=null)
    {
      print($node->getNext()->getData());
      if($node->getNext()->getNext()==null){break;}
      $node=$node->getNext();
    }
  }
   /**
  *@author MzXy
  *@param $data--要访问的节点的数据
  * @param 此方法只是演示不具有实际意义
  * 
  */
  public function getAt($data)
  {
    $node=$this->header->getNext();
     if($node->getNext()==null){
      print("数据集为空!");
      return;
    }
    while($node->getData()!=$data)
    {
      if($node->getNext()==null){break;}
      $node=$node->getNext();
    }
    return $node->getData();    
  }
   /**
  *@author MzXy
  *@param $value--需要更新的节点的原数据 --$initial---更新后的数据
  * 
  */
  public function update($initial,$value)
  {
     $node=$this->header->getNext();
     if($node->getNext()==null){
      print("数据集为空!");
      return;
    }
    while($node->getData()!=$data)
    {
      if($node->getNext()==null){break;}
      $node=$node->getNext();
    }
     $node->setData($initial);   
  }
}
?>

요약:위 내용은 이 글의 전체 내용입니다. 모든 분들의 공부에 도움이 되었으면 좋겠습니다.

관련 권장 사항:

php 중간 캐시 분류 데이터베이스 캐시

php 파일 업로드 관리 시스템

php는 CSV 형식 데이터 가져오기 및 내보내기 기능을 구현합니다.

위 내용은 PHP는 연결된 목록을 생성하고 연결된 목록 노드를 추가, 삭제, 업데이트 및 순회합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

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