HELP IN JAVA: Input is from the keyboard. Input integers, you don\'t know how ma
ID: 3746239 • Letter: H
Question
HELP IN JAVA: Input is from the keyboard. Input integers, you don't know how many but the last one will be zero. Put these integers into a list called list1 (not sorted, just put each new one at the end of the list. ) When you have finished creating your list, print it. Then create two new lists, one to hold all the even integers, one for the odds. Go through your original list deleting a node and adding it to either the odd or even list. Print out all three lists (the original one should now be empty).
Prompted the user for input
Used nodes and NOT a built in class
This criterion is linked to a Learning OutcomeInput from keyboard terminating in 0
Deleted nodes from first list
Added correctly to other lists
Printed out all three lists
Explanation / Answer
import java.util.Scanner;
class node
{
int d;
node next;
//constructor
node(int d)
{
this.d=d;
next=null;
}
}
class list
{
node l;
int c;
//method to insert
list()
{
c=0;
}
void insert(int d)
{
node n = new node(d);
n.next = l;
l=n;
c++;
}
int delete()
{
if(c==0)return -1;
node m = l;
l=l.next;
c--;
return m.d;
}
void display()
{
int i=0;
node t=l;
while(i<c)
{
System.out.print(t.d+" ");
i++;
t=t.next;
}
System.out.println();
}
}
public class Data_input {
public static void main(String argv[])
{
int n,i=0;
Scanner sc = new Scanner(System.in);
System.out.print(" Enter how many number you want to enter:");
n=sc.nextInt();
//object declaration:
list l = new list();
int m;
while(i<n)
{
System.out.print(" Enter number:");
m=sc.nextInt();
l.insert(m);
i++;
}
System.out.print(" Original list:");
l.display();//displaying list
list e = new list();
list o = new list();
i=0;
while(i<n)
{
m = l.delete();
if(m%2==0)//if even
{
e.insert(m);
}
else
{
o.insert(m);
}
i++;
}
System.out.print(" Original list:");
l.display();//displaying list
System.out.print(" Even number list::");
e.display();//displaying list
System.out.print(" Odd number list:");
o.display();//displaying list
}
}
output:
run:
Enter how many number you want to enter:5
Enter number:1
Enter number:2
Enter number:3
Enter number:4
Enter number:5
Original list:5 4 3 2 1
Original list:
Even number list::2 4
Odd number list:1 3 5
BUILD SUCCESSFUL (total time: 10 seconds)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.