Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
4th Edition
ISBN: 9780134787961
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 19, Problem 1AW

Explanation of Solution

Recursive method definition for “print”:

The recursive method definition for “print” is given below:

//Define recursive method for "print"

static void print(Node ref)

{

/* If the reference "ref" is not equal to "null", then */

    if (ref != null)

    {

        // Display the first value in the reference

        System.out.print(ref.value + " ");

//Recursively call the method "print" with next value of "ref"

        print(ref.next);

    }

}

Explanation:

From the above method,

  • If the reference “ref” is not equal to “null”, then display the first element in the reference.
  • And then recursively call the function “print” with the next value of “ref”.

Complete code:

The complete executable code for recursive method “print” is given below:

//Define "Example2LinkedList " class

public class Example2LinkedList

{

      //Define recursive method for "print"

    static void print(Node ref)

    {

Blurred answer
Students have asked these similar questions
Please give me the  proper answer with step by step solution Qns:Java question Implement a method that adds a value in a linked list using recursion and using this signature: public void addValue(int value)
By using JAVA, write a recursive display function, that print a linked list in reverse order.
Implement a recursive version of the size method for SinglyLinkedLists. (Hint: A wrapper may be useful.)
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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education