Question
Add a Node reference variable named lastNode to the LinkedList class. It will reference the last node in the linked list, also known as the
Add a Node reference variable named lastNode to the LinkedList class. It will reference the last node in the linked list, also known as the tail.
Make sure lastNode is always pointing to the end of the list. (Hint: you'll need to make changes to both the add and remove methods. Consider the cases when a Node is added to an empty list and when a Node is removed from a list with just one entry)
Add a new method addToEnd that allows a client to add a Node to the end of the list. This method should not traverse the list.
Add a new method addAfter which takes two parameters: a value to add to the linked list and the value it should be placed after in the linked list. For example, if we have the linked list "Cypress" -> "Fullerton" -> "Glendale" and the client calls addAfter("Fullerton", "Buena Park") the updated linked list should contain "Cypress" -> "Fullerton" -> "Buena Park" -> "Glendale". If the value to add after does not exist in the linked list, raise an exception.
Add a new method countOccurrences which takes a target value as a parameter. This method should return the number of times the target value appears in the linked list.
In your program's main method, demonstrate the creation of a LinkedList object and test the new methods you added.
You may use the following LinkedList class as a starting point for your assignment:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started