Search code examples
c++c++11linked-listnodesreset

Is this code for emptying a linked list okay?


I need to work on a function that resets a linked list without completely eliminating it, and I wanted to know if my piece of code is right. Any other suggestions?

        void reset(){
        if (initial == nullptr){
            return;
        }
        Node<T> *flag;
        Node<T> *temp;
        while(initial->obtainNext() != nullptr){
            flag = initial->();
            temp = flag->obtainNext();
            delete flag;
            inicial->setNext(temp);
        }
        initial->setNext(nullptr);
        return;
    }

Solution

  • By my understanding you're trying to erase your linked list. 1. You may just loop through the given node and delete all of them setting the head node as nullptr. 2. You may make an object which holds that linked list and may serve your purpose by using method 1 in erasing everybody but the object still exists with a variable set to nullptr.