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 19 Solutions
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
- use python Implement a recursive algorithm which will print all the elements of a non-dummy headed singly linked linear list in reversed order. Example: if the linked list contains 10, 20, 30 and 40, the method will print 40 30 20 10 Note: you’ll need a Singly Node class for this code.arrow_forwardWrite a code in python. Write down only the methods that are required; testers are not required. Complete the method below that takes the reference of a non-dummy headed singly linear linked list and removes all the nodes containing multiples of 5 from the list. remove_multiple_of_five(head)arrow_forwardWrite a method max() that takes a reference to the first node in a linked list asargument and returns the value of the maximum key in the list. Assume that all keys arepositive integers, and return 0 if the list is emptyarrow_forward
- Consider a singly linked list data structure as show below, implement the most efficient algorithm to detect whether the elements in the list form a palindrome or not, the method must return true/false accordingly. class SinglyList: class _Node: def __init__(self, e, next): self._element = e self._next = next def __init__(self): self._head = self._tail = None def is_palindrome(self)->bool:arrow_forwardImplement the complete code Delete that, when given an info value newElement, finds and deletes the first element with this value, if it exists. This involves also considering the four special cases listed for deleting a node from the beginning or the end of a linked list.arrow_forwardCreate a recursive method called isOrdered() that takes a Node and the two keys min and max as arguments. It should return true if all of the tree's keys are between min and max, min and max are, in fact, its smallest and largest keys, respectively, and the BST ordering property holds for all of the tree's keys, and false if it does not.arrow_forward
- Implement the inordermethod in BST using a stack instead of recursion. Write a test program thatprompts the user to enter 15 integers, stores them in a BST, and invokes theinorder method to display the elements.arrow_forwardImplement a class “LinkedList” which has two private data members head: A pointer to the Node class length: length of the linked listImplement the following private method: 1. bool InsertHead(int data); // Use InsertAt FunctionInsert at the start of the linked list. Return true2. bool InsertEnd(int data); // Use InsertAt FunctionInsert at the end of the linked list. Return true3. bool RemoveAt(int index);Remove and delete the node at position “index”. Return true if successful, otherwise, false. this in c++arrow_forwardWrite a Python program that creates a linked list with 10000 nodes. Each node has arandom number between -1000 and 1000. Write a Python program that sort the linked list of above question in ascending order using theinsertion sort.arrow_forward
- Implement removeFirst for a doubly linked listarrow_forwardhelp with this function C++ Write a recursive method, to be included in an unsorted linked list class, called greater, that will receive an x parameter. The method will print the info of nodes that contain a value that is greater than theparameter.arrow_forwardIn Java, Write a method to triple value (integer) of items (nodes) in a singly linked list.arrow_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