Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is a question that I\'m having trouble with in data structures can I get he

ID: 3603896 • Letter: H

Question

Here is a question that I'm having trouble with in data structures can I get help on this question:

Suppose that the variable head is a reference to the head node of a linked list of objects. Each node has an instance variable called link (which is a reference to the next node) and another instance variable called data (which is an Object that's stored in the node). Write a few lines of code to count the number of occurrences of a specific non-null target Object on the list. At the end of your code, a variable called answer should indicate how many times the target appears in the array. Use target.equals to test for equality.

Explanation / Answer

int answer = 0; // to store number of occurrences of target while(head!=null) { // break while loop when end of list is reached if (target.equals(head.data)) { // if data at current node is equal to head, increment answer by 1 answer = answer + 1; } head = head.link; // changing head to point to next node in the list }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote