Question: HELP IN JAVA: Create a circular (singly linked list) of ints using the file numbers.txt. You are to use SINGLY LINKED NODES and NOT the
HELP IN JAVA: Create a circular (singly linked list) of ints using the file numbers.txt.
You are to use SINGLY LINKED NODES and NOT the java Linked List class.
Pass this list to a method that deletes ever node whose item is an EVEN number.
Print the list. You should have only odd numbers remaining.
numbers.txt:
56 4 9 11 40 68 3 -3 55 40 30 100 90
Rubric:
Must be singly linked nodes
method to create initial CIRCULAR list
method to print CIRCULAR list
method to delete even numbers from CIRCULAR LIST
Must use modular code
What I have so far:
public class L4 { public static class Node { Object item; Node next; int data; Node(Object newItem) { item = newItem; next=null; } Node(Object newItem, Node nextNode) { item = newItem; next=nextNode; } }
public static Node addList(Node head, Node temp){ if (head == null) head = temp; else { Node previous = null; Node curr = head; while(curr != null){ previous = curr; curr = curr.next; } temp.next = curr; if(curr == head) //insert as first node head = temp; else previous.next = temp; } return head; } public static void PrintList(Node head) { Node curr = head; while (curr != null) { System.out.println(curr.item); curr = curr.next; } } public static void removeEvens(Node head){ Node prev = head; Node curr = head.next; while (curr != head){ if (curr.data % 2 == 0) { prev.next = curr.next; } else prev = curr; curr = curr.next; } } public static void main(String[] args) { Node head = null; L4 list = new L4(); File File1 = new File("numbers.txt"); Scanner fileInput = null; try { fileInput = new Scanner(File1); } catch (FileNotFoundException e) { } //While loop to get the contents of the file and into a student object while (fileInput.hasNext()) { //Creates a student object String a = fileInput.next(); //Adds the created student object into the list by calling thr addList method addList(head, new Node(a)); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
