Concept explainers
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)
{
Want to see the full answer?
Check out a sample textbook solutionChapter 20 Solutions
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
- Java language Write a method to insert an array of elements at index in a single linked list and then display this list. The method receives this array by parametersarrow_forwardWhat the code is about: Implement a recursive algorithm to add all the elements of a non-dummy headed singly linked linear list. Only head of the list will be given as parameter where you may assume every node can contain only integer as its element.Note: you’ll need a Singly Node class for this code. **PLEASE EXPLAIN HOW THE NODE CLASS AND THE CONSTRUCTOR OF THE NODE CLASS IS WORKING IN THIS CODE** #singlty node class for single linked listclass node: def __init__(self, value = None, next=None): self.value = value self.next = nextdef AddAll(head):#takes head of single linked list head if head==None: return 0#if reached end of the linked list return AddAll(head.next) + head.value #each node's next pointer is passed in recursive call #and value of each node is added while returning from recursive callarrow_forwardJava - Mileage for a Runnerarrow_forward
- java program java method: Write a method replace to be included in the class KWLinkedList (for doubly linked list) that accepts two parameters, searchItem and repItem of type E. The method searches for searchItem in the doubly linked list, if found then replace it with repItem and return true. If the searchItem is not found in the doubly linked list, then insert repItem at the end of the linked list and return false. Assume that the list is not empty. You can use ListIterator and its methods to search the searchItem in the list and replace it with repItem if found. Do not call any method of class KWLinkedList to add a new node at the end of the list. Method Heading: public boolean replace(E searchItem, E repItem) Example: searchItem: 15 repItem: 17 List (before method call): 9 10 15 20 4 5 6 List (after method call) : 9 10 17 20 4 5 6arrow_forwardGiven a singly linked list, print reverse of it using a recursive function printLinkedList( node *first ) where first is the pointer pointing to the first data node. For example, if the given linked list is 1->2->3->4, then output should be: 4 3 2 1 (note the whitespace in between each data value)arrow_forwardIn Java, a linked list always terminates with a node that is null. True O Falsearrow_forward
- Java help, can you please explain this I'm a beginner and I'm lost:( Implement a nested class DoubleNode for building doubly-linked lists, where each node containsa reference to the item preceding it and the item following it in the list (null if there is no suchitem). Then implement methods for the following tasks:• Print the contents of the list• Insert at the beginning• Insert at the end• Remove from the beginning• Remove from the end• Insert before a give node (Insert before the first occurrence of the node, if thenode exists; else insert at the end)• Insert after a given node (Insert after the first occurrence of the node, if the nodeexists; else insert at the end)• Remove a given node (Remove the first occurrence of the node, remove nothing ifnode not found)• Move to front (move the first occurrence of the node to the front)• Move to end (moved and first occurrence of the node to the end)Write a tester program and test your implementation. You must test each of these cases…arrow_forwardWrite a program to implement BST (Bindary Search Tree) by using linked list. Implement the following methods: 1) Insert 2)Search 3)The traversal method that gives the output in increasing order. Give name of method and implement the method. Code in C.arrow_forwardThis is recursive homework is being a bit tough please java code with comments. Thanksarrow_forward
- Think about the following example: A computer program builds and modifies a linked list like follows: Normally, the program would keep tabs on two unique nodes, which are as follows: An explanation of how to use the null reference in the linked list's node in two common circumstancesarrow_forwardint F(node<int>&p){int c=0; while(p!=0){p=p->next; c++; } return c;} This function is a. return the number of items in the linked list b. return the number of items in a linked list and destroy the linked list c. None of these d. destroy the list and free all allocated nodesarrow_forwardThink about the following example: A computer program builds and modifies a linked list like follows:Normally, the program would keep tabs on two unique nodes, which are as follows: An explanation of how to use the null reference in the linked list's node in two common circumstancesarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education