Write a java method for counting the elements of a singly linked list of integer
ID: 672247 • 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
package mani;
import java.util.Scanner;
public class Steady
{
public static void sumL(LinkedList list,int n){
String l=list.toString();
String[] elements=l.split(" ");
int count=0;
for(String i:elements){
if(Integer.parseInt(i)<5){
count=count+1;
}
}
System.out.println("The number of values lesser than"+n+" is "+count);
}
public static void main(String[] args){
Scanner s=new Scanner(System.in);
LinkedList list=new LinkedList();
System.out.println("Enter the integers into array: ");
for(int i=0;i<9;i++){
System.out.print((i+1)+" element: ");
int n=s.nextInt();
list.add(n);
}
System.out.println(list.toString());
System.out.println("Enter the number : ");
int sum=s.nextInt();
sumL(list,sum);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.