Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

// 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Azure SQL Database For Modern Developers Building Applications In The Microsoft Cloud

Authors: Davide Mauri, Silvano Coriani, Anna Hoffma, Sanjay Mishra, Jovan Popovic

1st Edition

1484263693, 978-1484263693

More Books

Students also viewed these Databases questions

Question

Is how things are said consistent with what is said?

Answered: 1 week ago

Question

What are Decision Trees?

Answered: 1 week ago

Question

What is meant by the Term Glass Ceiling?

Answered: 1 week ago