Error gotten from trying to use recursion in my find and print function in my double linkedList class in C++

-2

I am trying to write a find and print function for a double linked list class using recursion. The code works for some parts and then breaks after a little while. This is the error I am getting. Error: Unhandled exception at 0x678AC41D (ucrtbased.dll) in DoubleLinkedList.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x00402FFC). occurred

Please can anyone help? Here is my code. Thanks!

    node<T>* head;

    dLinkedList()
    {
        head = nullptr;
    }

    void insert(T item)
    {
        node<T>* temp = new node<T>(item);
        if (head != nullptr)
        {
            head->prev = temp;
            temp->next = head;
            head = temp;
        }
        else
        {
            head = temp;
        }
    }

    bool find(T item)
    {
        /*node<T>* temp = head;
        for (temp = head; temp->next != nullptr; temp = temp->next)
        {
            if (temp->data == item) return true;
        }
        if (temp->data == item) return true;

        return false;*/
        return find(item, head);
    }

    bool find(T item, node<T>* root)
    {
        if (root == nullptr) return false;

        if (root->data == item) return true;

        return find(item, root->next);
    }

    void print()
    {
        /*node<T>* temp = head;
        for (temp = head; temp->next != nullptr; temp = temp->next)
        {
            cout << temp->data << "\n";
        }
        cout << temp->data << "\n";*/

        print(head);
    }

    bool print(node<T>* root)
    {
        if (root != nullptr)
        {
            cout << root->data << "\n";
            return print(root->next);
        }
        else
        {
            cout << "Ended at nullptr";
            return true;
        }
    }
};
#endif //DLINKEDLIST
c++
recursion
stack-overflow
doubly-linked-list
unhandled-exception
asked on Stack Overflow Aug 18, 2020 by Ore2000 • edited Aug 18, 2020 by drescherjm

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0