Question

I'd like to make this portion of my project a recursive function... how can I accomplish...

I'd like to make this portion of my project a recursive function... how can I accomplish this? This is what I have now. Thank you!!

// A wrapper to a recursive method that inverts the contents of the list
// @post the contents of the list are inverted such that
// the item previously at position 1 is at position item_count_,
// the item previously at position 2 is at position item_count_-1 ...
// the item previously at position ⌊item_count/2⌋ is at position
template<class T>
void LinkedList<T>::invert() {
    // Initialize current, previous and next pointers
    Node<T>* current = head_ptr_;
    Node<T>* prev = nullptr, *next_ = nullptr;


    while (current != nullptr)
    {
        // SAVE NEXT
        next_ = current->next_;

        // Flip the current NODE's POINTER
        current->next_ = prev;

        // Move POINTER one position ahead, by PUSHING IT ONE UP = NEXT
        prev = current;
        current = next_;
    }
    head_ptr_ = prev;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
// this method takes a starting node, and reverse the 
// list starting from that.
// the result is the head of the resutant list.
// end is passed as reference, which gets filled in the 
// code.
template<class T>
Node<T>* LinkedList<T>::reverse(Node<T>* start, Node<T>* &end) {
        // if this is the only node in the list
        if(start == nullptr || start->next_ == nullptr) {
                end = start; // assign the node as end of list too.
                return start;
        }
        
        Node<T> *current = start;
        
        // reverse 
        Node<T> *result = reverse(start->next_, end);
        current->next_ = nullptr;
        end-next_ = current;
        return result;
}

template<class T>
void LinkedList<T>::invert() {
        Node<T>* end;
        
        // call recursive method.
        head_ptr_ = reverse(head_ptr_, end);
}


Please upvote, as i have given the exact answer as asked in question. Still in case of any concerns in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
I'd like to make this portion of my project a recursive function... how can I accomplish...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is a c++ class utilizing class templates and linked lists. I need to implement the...

    This is a c++ class utilizing class templates and linked lists. I need to implement the following member function(s) to List.cpp. Node.hpp/cpp should be fine but if you feel like there needs to be a change for compilation or testing, feel free to do so but make sure to comment on why it was done. /** @pre assumes position is valid, if position is > item_count_ it returns an empty List, also assumes that operators <= and >= are defined...

  • Using the provided Linked List template, add the following recursive functions and demonstrate them in a...

    Using the provided Linked List template, add the following recursive functions and demonstrate them in a separate cpp file. Write a recursive function to print the list in order. Write a recursive function to print the list in reverse order. Write a recursive function to print every other node in the list in order. Write a recursive function to return the number of nodes in the list. Write a Boolean function that implements the recursive version of sequential search. THIS...

  • C programming A linked list is a linear data structure that allows us to add and remove items fro...

    c programming A linked list is a linear data structure that allows us to add and remove items from the list very quickly, by simply changing a few pointers. There are many different variations of linked lists. We have studied the doubly-linked, circular, with a dummy-header-node version of a linked list. In the class notes we studied several functions to manipulate a Linked List. For this assignment you must write the code for the following additional linked list functions: addFirst,...

  • can someone please double check my code here are the requirements please help me fulfill the...

    can someone please double check my code here are the requirements please help me fulfill the requirements Using the material in the textbook (NumberList) as a sample, design your own dynamic linked list class (using pointers) to hold a series of capital letters. The class should have the following member functions: append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges...

  • I need this in C++. This is all one question Program 2: Linked List Class For...

    I need this in C++. This is all one question Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we'll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

  • please help!!!! JAVA I done the project expect one part but I still give you all...

    please help!!!! JAVA I done the project expect one part but I still give you all the detail that you needed... and I will post my code please help me fix the CreateGrid() part in main and make GUI works    List Type Data Structures Overview : You will be implementing my version of a linked list. This is a linked list which has possible sublists descending from each node. These sublists are used to group together all nodes which...

  • Please provide original Answer, I can not turn in the same as my classmate. thanks In...

    Please provide original Answer, I can not turn in the same as my classmate. thanks In this homework, you will implement a single linked list to store a list of computer science textbooks. Every book has a title, author, and an ISBN number. You will create 2 classes: Textbook and Library. Textbook class should have all above attributes and also a “next” pointer. Textbook Type Attribute String title String author String ISBN Textbook* next Textbook Type Attribute String title String...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT