Question: Exercise 3. Add the following method to LinkedList.java. Method public void enumerateOddNodes()that prints the contents of all the nodes with odd indices. Exercise 4. Add
Exercise 3. Add the following method to LinkedList.java. Method public void enumerateOddNodes()that prints the contents of all the nodes with odd indices.
Exercise 4. Add the following method to LinkedList.java. Method public void listAllNodesWith(String d) that displays the indices of all the nodes that have the String d in them
*I am using JGrasp so please make sure it is compatible *Please comment throughout code *FULL QUESTION IN IMAGES BELOW
CODE FOR NODE, LINKED LIST AND LINKED LIST DEMO PROVIDED:
//class Node
public class Node{
private String data; private Node next; public Node(String d, Node n){
data = d;
next = n; }
public String getData(){ return data;
} public Node getNext(){
return next; }
public void setData(String d){ data = d;
} public void setNext(Node n){
next = n; }
public String toString(){ return data + "-->";
} }
//class LinkedList (only the first few methods are given here)
public class LinkedList{
private Node front; private int count;
//constructor
public LinkedList(){ front = null; count = 0;
}
//add a node to the front of the linked list
public void addToFront(String d){ Node n;
n = new Node(d, front); front = n; count++;
}
//get the current size of the list
public int size(){ return count;
}
//check if the list is empty
public boolean isEmpty(){ return (count==0);
}
//clear the list
public void clear(){ front = null; count=0;
}
//get the content of the first node
public String getFrontData() {
if (front==null) return "Empty list";
else return front.getData();
}
/ew method added - get the first node
public Node getFront() {
return front; }
//scan the list and print contents
public void enumerate() {
Node curr = front; while (curr!=null) {
System.out.print(curr);
curr = curr.getNext(); }
System.out.println("."); }
}
//class LinkedListDemo1
public class LinkedListDemo1 {
public static void main(String[] args) {
LinkedList myList = new LinkedList(); myList.addToFront("Toronto");
myList.addToFront("New York"); myList.addToFront("Calgary");
myList.addToFront("Halifax"); myList.addToFront("St.John's"); System.out.println("Number of nodes in the list: "+ myList.size()); myList.enumerate(); myList.addToFront("Vancouver"); myList.addToFront("Montreal"); myList.enumerate();
}
}



LINKED LISTS Before you solve Exercises 3 and 4, test the Node class and the LinkedList class given below with the LinkedListDemol class (also given below). Understand the basics of Linked Lists. //class Node public class Node private String data private Node next; public Node (String d, Node n) data d; next n; public String getData return data public Node getNext return next public void setData (String d) data d public void set Next (Node n) next E n public String to String return data //class LinkedList (only the first few methods are given here) public class LinkedList{ private Node front; private int count
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
