Write a recursive function called print list() that outputs the integer value of each node in a linked list. Function print_list() has one parameter, the head node of a list. The main program reads the size of the linked list, followed by the values in the list. Assume the linked list has at least 1 node.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 6PE
icon
Related questions
icon
Concept explainers
Question

code used:

python

 

class Node:
    def __init__(self, value):
        self.data_val = value
        self.next_node = None

    def insert_after(self, node):
        tmp_node = self.next_node
        self.next_node = node
        node.next_node = tmp_node

    def get_next(self):
        return self.next_node

    def print_data(self):
        print(self.data_val, end=", ")

# TODO: Write recursive print_list() function here.

        
if __name__ == "__main__":
    size = int(input())
    value = int(input())
    head_node = Node(value) # Make head node as the first node
    last_node = head_node
    
    # Insert the second and the rest of the nodes
    for n in range(1, size):
        value = int(input())
        new_node = Node(value)
        last_node.insert_after(new_node)
        last_node = new_node
    
    print_list(head_node)

Write a recursive function called print_list() that outputs the integer value of each node in a linked list. Function print_list() has one
parameter, the head node of a list. The main program reads the size of the linked list, followed by the values in the list. Assume the linked
list has at least 1 node.
Ex: If the input of the program is:
5
1.
2
3
4
5
the output of the print_list() function is:
1, 2, 3, 4, 5,
Hint: Output the value of the current node, then call the print_list() function repeatedly until the end of the list is reached. Refer to the Node
class to explore any available instance methods that can be used for implementing the print_list() function.
Transcribed Image Text:Write a recursive function called print_list() that outputs the integer value of each node in a linked list. Function print_list() has one parameter, the head node of a list. The main program reads the size of the linked list, followed by the values in the list. Assume the linked list has at least 1 node. Ex: If the input of the program is: 5 1. 2 3 4 5 the output of the print_list() function is: 1, 2, 3, 4, 5, Hint: Output the value of the current node, then call the print_list() function repeatedly until the end of the list is reached. Refer to the Node class to explore any available instance methods that can be used for implementing the print_list() function.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Depth First Search
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
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr

Expert Answers to Latest Homework Questions

Q: Provide cited references of journals, books materials on the French language.
Q: (h) Journalize and post closing entries and complete the closing process. (Credit account titles are…
Q: Problem 7.9 At a sudden contraction in a pipe, the diameter changes from D₁ to D2. The pressure…
Q: Which approach is SAFEST for the synthesis of the following ester: معه a. C. d. + CH₂OH b. ང་ཐིག་, -…
Q: 42. Consider the random variable (X, Y) of Exercise 9. (a) Find the curve of regression of X on Y.…
Q: Chapter 23 Homework i 2 Saved Complete the following partial flexible budget performance report, and…
Q: Photo 5 Examine how light was used in the photo. Trace the steps from light source to camera:
Q: Diagram 5 Use the following objects and arrows to diagram and label the path of light: SONY
Q: Which of the following could be a reason for the type of market failure known as information…
Q: I need help on this question?
Q: i need please
Q: An oxygen ion with a charge of - 2e is released from rest near the negative plate of a capacitor…
Q: i need help please it hard
Q: A proton moves in a circular orbit 150 mm in radius that is perpendicular to a uniform 0.25…
Q: Predict the product of the following reactions OH Br2 H HO NaCN TSOH hv 1) LiAlH4 2) H₂O acid ?
Q: Please provide Answer with calculation
Q: Answer please with correct option
Q: 0.75m, and 73 = Consider the circuit shown below. Given that the battery provides 9.0V, r₁ = 0.5m,…
Q: Problem 11-08 Shown below are the investment weights for the securities held in four different…
Q: Propose the reagents in best order for the synthesis of the following compound from benzene. CI A)…
Q: When a country opens its markets to international trade, if the world price is…