用户工具

站点工具


链表
定义链表 
class ListNode { 
   public $val = NULL; 
   public $next = NULL; 
 function __construct($val = 0, $next = null) {
         $this->val = $val;
         $this->next = $next;
    }
}
 
 
class LinkList { 
   public $next=null;
   public function insert($val = NULL) { 
      $newNode = new ListNode($val); 
      if ($this->next === NULL) {           
          $this->next = $newNode;             
      } else { 
          $currentNode = $this->next; 
          while ($currentNode->next !== NULL) { 
              $currentNode = $currentNode->next; 
       } 
          $currentNode->next = $newNode; 
      } 
  }
 public function insert_arr($arr = array() ) { 
     foreach($arr as $val){
        $this->insert($val);  
     }

}

public function display() { 
echo "当前链表的是: <br/>"; 
      $currentNode = $this->next; 
      while ($currentNode !== NULL) { 
          echo $currentNode->val . "\n"; 
          $currentNode = $currentNode->next; 
      } 
  } 
} 
/home/minxuanbm4ipnyxlu3ann/wwwroot/dokuwiki/data/pages/链表.txt · 最后更改: 2026/01/27 16:27 由 116.204.64.97