Question: // A LinkedList object can be used to store a list of objects. public class LinkedList { private ListNode front; // node holding first value

 // A LinkedList object can be used to store a list

// A LinkedList object can be used to store a list of objects. public class LinkedList { private ListNode front; // node holding first value in list (null if empty) private String name = "front"; // string to print for front of list // Constructs an empty list. public LinkedList() { front = null; } // Constructs a list containing the given elements. // For quick initialization via Practice-It test cases. @SuppressWarnings("unchecked") public LinkedList(E... elements) { if (elements.length > 0) { front = new ListNode(elements[0]); ListNode current = front; for (int i = 1; i  [" + current.data + "]"; if (current.cycle) { result += " (cycle!)"; cycle = true; break; } current = current.__gotoNext(); } if (!cycle) { result += " /"; } return result; } // Returns a text representation of the list. public String toString() { return toFormattedString(); } // ListNode is a class for storing a single node of a linked list. This // node class is for a list of values. // Most of the icky code is related to the task of figuring out // if the student has accidentally created a cycle by pointing a later part of the list back to an earlier part. private class ListNode { public E data; // data stored in this node public ListNode next; // link to next node in the list public boolean visited; // has this node been seen yet? public boolean cycle; // is there a cycle at this node? // post: constructs a node with data 0 and null link public ListNode() { this(null, null); } // post: constructs a node with given data and null link public ListNode(E data) { this(data, null); } // post: constructs a node with given data and given link public ListNode(E data, ListNode next) { ALL_NODES.add(this); this.data = data; this.next = next; this.visited = false; this.cycle = false; } public ListNode __gotoNext() { return __gotoNext(true); } public ListNode __gotoNext(boolean checkForCycle) { if (checkForCycle) { visited = true; if (next != null) { if (next.visited) { // throw new IllegalStateException("cycle detected in list"); next.cycle = true; } next.visited = true; } } return next; } } private final java.util.List ALL_NODES = new java.util.ArrayList(); public void clearCycleData() { for (ListNode node : ALL_NODES) { node.visited = false; node.cycle = false; } } // YOUR CODE GOES HERE }

O count Language/Type: Related Links: Author: Java generics implementing instance methods Linked List Linked List.java Marty Stepp (on 2012/02/29) Write a method count that accepts an element value as a parameter and returns the number of occurrences of that value in the list. For example, suppose a variable named list stores the following sequence of elements: [one, two, three, two, four, two, five, two, two, six] A call of list.count("two") should return 5 because there are five occurrences of that value in the list. If the list does not contain the value at all, return O. The example shown is a list of strings, but this is a generic linked list class that can store any type of objects. Assume that you are adding this method to the LinkedList class as defined below: public class LinkedList { private ListNode front; // null for an empty list } X Type your solution here: public static int count(E a) { warning: [unchecked] Possible heap pollution from parameterized vararg type E 2 int count = 0; ListNode temp = front; while (temp != null) { 5 if (temp.data.equals(a)) { 6 count++; 7 } 8 9 return count; 10 } This is a partial class problem. Submit code that will become part of an existing Java class as described. You do not need to write the complete class, just the portion described in the problem. Submit Indent Sound F/X Highlighting Your code did not compile. Please read and correct the errors below. Error emitted from internal testing code. This error is occurring when our internal testing code is trying to call your code. If you see this, it may mean that you have the wrong header for your solution, or a mismatched { } (brace. warning: [unchecked] Possible heap pollution from parameterized vararg type E warning: [unchecked] Possible heap pollution from parameterized vararg type E public void _helper(E value, int expectedCount, E... elements) { where E is a type-variable: E extends Object declared in method _helper(E, int, E...)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!