繁体   English   中英

为什么在此php代码中出现致命错误:

[英]Why am I getting Fatal error: in this php code?

出现以下错误。 不知道为什么

致命错误:调用未定义的函数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";



?>

您可以使用$this关键字访问相同的类函数

在131行替换

reverse_list_iteratively($current->next);

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

说明

reverse_list_iteratively()是一个类函数(并且它不是静态的),因此您需要对象来访问此函数,对于同一类,您可以使用$ this关键字进行访问

您的通话没有参数:

$list->reverse_list_recursively();

并且您的定义采用1个参数:

public function reverse_list_recursively($current = NULL)

我怀疑这可能是问题。

在这种情况下,您已经使用参数定义了函数,但是没有传递任何内容,因此它正在查找参数。 你可以试试这个

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM