Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question

I need help with fixing this java program as described in the image below:

LablProgram.java:

import java.util.Scanner;

public class LabProgram {
   
   /* TODO: Write recursive printLinkedList() method here. */
   
   
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      int size;
      int value;
      
      size = scnr.nextInt();
      value = scnr.nextInt();
      IntNode headNode = new IntNode(value); // Make head node as the first node
      IntNode lastNode = headNode;      // Node to add after
      IntNode newNode = null;           // Node to create
      
      // Insert the second and the rest of the nodes
      for (int n = 0; n < size - 1; ++n) {
         value = scnr.nextInt();
         newNode = new IntNode(value);
         lastNode.insertAfter(newNode);
         lastNode = newNode;
      }
      
      // Call printLinkedList() with the head node
      printLinkedList(headNode);
   }
}

 

intNode.java:

public class IntNode {
   int dataVal;
   private IntNode nextNodeRef; // Reference to the next node
   
   // Constructor
   public IntNode(int value) {
      this.dataVal = value;
      this.nextNodeRef = null;
   }
   
   // Insert a new node after the current node
   public void insertAfter(IntNode nodeLoc) {
      IntNode tmpNext;
      
      tmpNext = this.nextNodeRef;
      this.nextNodeRef = nodeLoc;
      nodeLoc.nextNodeRef = tmpNext;
   }
   
   // Get location pointed by nextNodeRef
   public IntNode getNext() {
      return this.nextNodeRef;
   }
   
   // Print the node's data
   public void printData() {
      System.out.printf("%d, ", this.dataVal);
   }
}

Write a recursive method called printLinked List () that outputs the integer value of each node in a linked list. Method printLinkedList() 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 printLinked List () method is:
1, 2, 3, 4, 5,
Hint: Output the value of the current node, then call the printLinkedList() method repeatedly until the end of the list is reached. Refer to the
IntNode class to explore any available member methods that can be used for implementing the printLinkedList() method.
expand button
Transcribed Image Text:Write a recursive method called printLinked List () that outputs the integer value of each node in a linked list. Method printLinkedList() 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 printLinked List () method is: 1, 2, 3, 4, 5, Hint: Output the value of the current node, then call the printLinkedList() method repeatedly until the end of the list is reached. Refer to the IntNode class to explore any available member methods that can be used for implementing the printLinkedList() method.
Expert Solution
Check Mark
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
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