简体   繁体   中英

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

Getting the following error. Not sure why

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";



?>

You can access same class function using $this keyword

at line 131 replace

reverse_list_iteratively($current->next);

with

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

Explanation

reverse_list_iteratively() is a class function(and its not static), so you need object to access this function, for same class you can access with $ this keyword

your call is done with no parameter:

$list->reverse_list_recursively();

and your definition takes 1 parameter:

public function reverse_list_recursively($current = NULL)

I am suspecting that this could be issue.

in this case you have defined the function with an argument but you are passing nothing so it is finding the argument. you can try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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