获得以下错误。不确定为什么
致命错误:调用未定义的函数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 :(得分:5)
您可以使用$this
关键字
替换
reverse_list_iteratively($current->next);
带
$this->reverse_list_iteratively($current->next);
<强>解释强>
reverse_list_iteratively()是一个类函数(它不是静态的),因此您需要对象来访问此函数,对于可以使用$ this
关键字访问的同一个类
答案 1 :(得分:0)
您的通话没有参数:
$list->reverse_list_recursively();
,您的定义需要1个参数:
public function reverse_list_recursively($current = NULL)
我怀疑这可能是个问题。
答案 2 :(得分:0)
在这种情况下,您已使用参数定义了该函数,但您没有传递任何内容,因此它正在查找参数。你可以试试这个
echo "Reversing the list rec";
$list->reverse_list_recursively(0);
$list->print_list();
echo "\n";