Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a java method for counting the elements of a singly linked list of integer

ID: 671306 • Letter: W

Question

Write a java method for counting the elements of a singly linked list of integers that are less than a given value. The driver class provided will guide on the name and parameters of the method. Create a class(named Steady) in which the method will reside rather than modify the linkedlist. The method must make use of the Node/linkedlist framework provided. Arrays should not be used. The first value of the input is used as a parameter to the method. The rest are used to construct the lis upon which the method is performed.

Sample I/O:

5 1 9 7 12 3 4 20 2

List: 1 9 7 12 3 4 20 2

Then number of values less than 5 is 4.

public class LinkedList {
protected Node head;

public LinkedList () { head = null; }

public void add ( int number ) {
Node t = new Node (number);
t.setNext (head);
head = t;
}

public String toString () {
Node t = head;
if (t==null) {
return "";
}
else {
String list = t.toString();
while (t.hasNext()) {
t=t.getNext();
list=list+" "+t.toString();
}
return list;
}
}
}

public class Node {
private int data;
private Node next;

Node(int number) { data=number; }

public Node getNext() { return next; }
public void setNext(Node n) { next=n; }
public boolean hasNext() { return next!=null; }

public int getData() { return data; }
public void setData(int number) { data=number; }   

public String toString () { return ""+data; }
}

import java.util.Scanner;
//
public class Fever {
  
private Fever() { /* Class does not define a type of object. */}
  
private static void loadList(final Scanner scanner, final LinkedList list) {
if (scanner.hasNextInt()) {
final int value = scanner.nextInt();
loadList(scanner, list);
list.add(value);
}
}
  
public static void main(final String[] args) {
  
// Create a scanner that processes a single line of input.
Scanner scanner = new Scanner((new Scanner(System.in)).nextLine());
  
// First value is the test argument to be used in the method.
final int testArg = scanner.nextInt();
// Rest of the values form the list.
final LinkedList list = new Solution();
loadList(scanner, list);
  
scanner.close();
  
// Print list.
System.out.println("List: "+list);
// Execute method and print result.
System.out.print("The number of values less than "+testArg+" is ");
final int sum = ((Solution)list).numLessThan(testArg);
System.out.println(sum+".");
}
}

Explanation / Answer

public class LinkedList {
protected Node head;

public LinkedList () { head = null; }

public void add ( int number ) {
Node t = new Node (number);
t.setNext (head);
head = t;
}

public String toString () {
Node t = head;
if (t==null) {
return "";
}
else {
String list = t.toString();
while (t.hasNext()) {
t=t.getNext();
list=list+" "+t.toString();
}
return list;
}
}
}

public class Node {
private int data;
private Node next;

Node(int number) { data=number; }

public Node getNext() { return next; }
public void setNext(Node n) { next=n; }
public boolean hasNext() { return next!=null; }

public int getData() { return data; }
public void setData(int number) { data=number; }   

public String toString () { return ""+data; }
}

import java.util.Scanner;
//
public class Fever {
  
private Fever() { /* Class does not define a type of object. */}
  
private static void loadList(final Scanner scanner, final LinkedList list) {
if (scanner.hasNextInt()) {
final int value = scanner.nextInt();
loadList(scanner, list);
list.add(value);
}
}
  
public static void main(final String[] args) {
  
// Create a scanner that processes a single line of input.
Scanner scanner = new Scanner((new Scanner(System.in)).nextLine());
  
// First value is the test argument to be used in the method.
final int testArg = scanner.nextInt();
// Rest of the values form the list.
final LinkedList list = new Solution();
loadList(scanner, list);
  
scanner.close();
  
// Print list.
System.out.println("List: "+list);
// Execute method and print result.
System.out.print("The number of values less than "+testArg+" is ");
final int sum = ((Solution)list).numLessThan(testArg);
System.out.println(sum+".");
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote