Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
3rd Edition
ISBN: 9780134038179
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 20, Problem 3AW

Explanation of Solution

Method definition for “reverse()”:

The recursive method definition for “reverse()” is given below:

/* Recursive method definition for "reverse" with parameter */

private Node reverse(Node list)

{

    //Create a node "newList"

    Node newList;

/* If the given node is null or last node is null, then */

    if((list == null) || (list.next == null))

    {

        /* Return the node */

        return list;

    }

/* Recursively call the method "reverse" for remaining node */

    newList = reverse(list.next);

    //Modify references for middle sequence

    list.next.next = list;

    list.next = null;

    //Return new head node in each recursion

    return newList;

}

/* Recursive Method definition for reverse method without parameter */

private void reverse()

{

    /* Call the method "reverse" with list head */

    first = reverse(first);

}

Explanation:

The above method definition is used to reverse the elements in a list.

  • Recursive method definition of “reverse()” with an parameter “list”.
    • Create a node “newList”.
    • If the given node is null or last node is null, then return the node.
    • Recursively call the method “reverse” for remaining node.
    • Modify references for middle sequence.
    • Finally, return new head node in each recursion.
  • Recursive method definition of “reverse()” without parameter.
    • Call the method “reverse” with list head.

Complete code:

The complete executable code for reverse the elements in a list using recursive “reverse()” method is given below:

//Define "LinkedList1" class

class LinkedList1

{

/** The code for this part is same as the textbook of "LinkedList1" class */  

/* Recursive method definition for "reverse" with parameter */

    private Node reverse(Node list)

    {

        //Create a node "newList"

        Node newList;

/* If the given node is null or last node is null, then */

        if((list == null) || (list.next == null))

        {

            /* Return the node */

            return list;

        }

/* Recursively call the method "reverse" for remaining node */

        newList = reverse(list...

Blurred answer
Students have asked these similar questions
Implement a recursive function void deleteMax() on the IntList class (provided). The function will delete from the IntList the IntNode containing the largest value. If there are multiple nodes containing this largest value, only delete the 1st one. Be careful not to cause any memory leaks or dangling pointers. You may NOT use any kind of loop (must use recursion). You may NOT use global or static variables. You may NOT use any standard library functions.   Ex: list: 5->7->1->16->4->16->3 list.deleteMax(); list: 5->7->1->4->16->3   IntList.h #ifndef __INTLIST_H__#define __INTLIST_H__ #include <ostream> using namespace std; struct IntNode {int value;IntNode *next;IntNode(int value) : value(value), next(nullptr) {}}; class IntList { private:IntNode *head; public: /* Initializes an empty list.*/IntList() : head(nullptr) {} /* Inserts a data value to the front of the list.*/void push_front(int val) {if (!head) {head = new IntNode(val);} else {IntNode…
This is recursive homework is being a bit tough please java code with comments.   Thanks
Write and implement a recursive version of the binary search algorithm. Also, write a version of the sequential search algorithm that can be applied to sorted lists. Add this operation to the class orderedArrayListType for array-based lists. Moreover, write a test program to test your algorithm.
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning