2015-01-17 29 views
0

獲取以下錯誤。不知道爲什麼爲什麼我得到致命錯誤:在這個php代碼中?

Fatal error: Call to undefined function reverse_list_recursively()

<?php 

class ListNode { 
    public $data; 
    public $next; 

    function __construct($data) { 
     $this->data = $data; 
     $this->next = NULL; 
    } 

    function getData() { 
     return $this->data; 
    } 
} 

class LinkList { 

    // points to the first node 
    private $head; 

    // node count 
    private $count; 

    function __construct() { 
     $this->head = NULL; 
     $this->count = 0; 
    } 

    // checks if the list is empty or not 
    function is_empty() { 
     return ($this->head == NULL); 
    } 

    // inserts data at the beginning of the list 
    public function insert_beg($data) { 

     $link = new ListNode($data); 
     $link->next = $this->head; 
     $this->head = &$link; 
     $this->count++; 
    } 

    public function insert_last($data) { 
     $current = $this->head; 

     if($current == NULL) { 
      $this->insert_beg($data); 

     } else { 

      while($current->next != NULL) { 
       $current = $current->next; 
      } 
      $link = new ListNode($data); 
      $current->next = &$link; 
      $link->next = NULL; 
      $this->count++; 
     } 
    } 

    public function delete_first_node() { 
     if($this->head != NULL) { 
      if($this->head->next != NULL) { 
       $this->head = $this->head->next; 
       $this->count--; 
      } 
     } 
    } 

    public function delete_last_node() { 

     $current = $this->head; 
     if($current != NULL) { 

      $prev = $current; 
      while($current->next != NULL) { 
       $prev = $current; 
       $current = $current->next; 
      } 
      $current = $prev; 
      $current->next = NULL; 
      $this->count--; 
     } 
    } 

    public function delete_node($data) { 
     $current = $prev = $this->head; 
     if ($current == NULL) { 
      return; 
     } else { 
      while($current->data != $data && $current->next != NULL) { 
       $prev = $current; 
       $current = $current->next; 
      } 
      if($current->data == $data) { 
       $prev->next = $current->next; 
       $this->count--; 
      } 
      return; 
     } 
    } 

    public function reverse_list_iteratively() { 
     $current = $this->head; 

     // if the list is empty or only one element in the list return 
     if($current == NULL || $current->next == NULL) { 
      return; 
     } else { 
      $next = $prev = NULL; 
      while($current != NULL) { 
       $next = $current->next; 
       $current->next = $prev; 
       $prev = $current; 
       $current = $next; 

      } 
      $this->head = $prev; 
     } 
    } 

    public function reverse_list_recursively($current = NULL) { 
     $current = $this->head; 

     // base case when the current is empty 
     if($current->next == NULL) { 
      $this->head = $current; 
      return $current; 
     } else { 
      reverse_list_recursively($current->next); 
      $current->next->next = $current; 
      $current->next = NULL; 
     } 

    } 

    public function print_list() { 
     $current = $this->head; 
     echo "\nThe list is: "; 
     while($current != NULL) { 
      echo "$current->data" . " "; 
      $current = $current->next; 
     } 
    } 

    public function get_size() { 
     return $this->count; 
    } 

} 
    $totalNodes = 10; 

    $list = new LinkList(); 


    echo "Is list empty before adding nodes: "; 
    var_export($list->is_empty()); 
    echo "\n"; 

    for($i=1; $i <= $totalNodes; $i++) { 
     $list->insert_last($i); 
    } 

    echo "Is list empty after adding nodes: "; 
    var_export($list->is_empty()); 
    echo "\n"; 

    echo "Size of the list: " . $list->get_size(); 
    echo "\n"; 

    echo "List is: "; 
    $list->print_list(); 
    echo "\n"; 


    echo "Deleting first node: "; 
    $list->delete_first_node(); 
    $list->print_list(); 
    echo "\n"; 

    echo "Deleting last node: "; 
    $list->delete_last_node(); 
    $list->print_list(); 
    echo "\n"; 

    echo "Deleting node 6: "; 
    $list->delete_node(6); 
    $list->print_list(); 
    echo "\n"; 

    echo "Reversing the list iteratively"; 
    $list->reverse_list_iteratively(); 
    $list->print_list(); 
    echo "\n"; 

    echo "Reversing the list rec"; 
    $list->reverse_list_recursively(); 
    $list->print_list(); 
    echo "\n"; 



?> 
+0

這很奇怪,你的代碼對我來說似乎很好。 – dhidy

回答

0

您的通話,沒有參數的做法:

$list->reverse_list_recursively(); 

和你的定義需要1個參數:

public function reverse_list_recursively($current = NULL) 

我懷疑這可能是問題。

+0

我有一個默認值集,當我通過一些東西時,我仍然得到相同的錯誤。 – CodeCrack

5

可以使用$this關鍵字訪問相同的類功能

在線路131與

$this->reverse_list_iteratively($current->next); 

說明

reverse_list_iteratively取代

reverse_list_iteratively($current->next); 

()是一個類的函數(它不是靜態的),所以你需要對象來訪問這個函數,對於同一個類你可以訪問$ this關鍵字

0

在這種情況下,你已經定義了一個參數的函數,但你沒有傳遞任何東西它找到了論點。你可以試試這個

echo "Reversing the list rec"; 
$list->reverse_list_recursively(0); 
$list->print_list(); 
echo "\n"; 
+0

他分配了一個默認值NULL – Rafael

相關問題