Question
HELP WITH LINKED LIST METHODS IN JAVA Problem 1 Enhance the LinkedList class by adding following methods to do some more operations of the linked
HELP WITH LINKED LIST METHODS IN JAVA
Problem 1
Enhance the LinkedList class by adding following methods to do some more operations of the linked list:
insertAt(pos, data): inserts the element data at position pos of the list. If position exceeds the length of the list throw an exception.
Example: List: 4 6 7 2
insertAt(2, 8) - List: 4 6 8 7 2
insertAt(0, 9) - List: 9 4 6 8 7 2 insertAt(-1, 10) - IllegalArgumentException - List: 9 4 6 8 7 2
insertAt(8, 12) - IndexOutOfBoundsException -List: 9 4 6 8 7 2
removeAt(pos): remove element at position pos. If position exceeds the length of the list throw an exception. Example:
List: 4 6 7 2 5 9 removeAt(2) List: 4 6 2 5 9
removeAt(0) List: 6 2 5 9
removeAt(-1) IllegalArgumentException List: 6 2 5 9 removeAt(7) IndexOutOfBoundsException List: 6 2 5 9
insertAfter(key, data): insert a new element data after the first occurrence of key.
Example:
List: 4 6 7 2 5 9
insertAfter(2, 3) List: 4 6 7 2 3 5 9
insertAfter(1, 8) IndexOutOfBoundsException List: 4 6 7 2 3 5 9
insertBefore(key, data): insert a new element data before the first occurrence of key.
Example:
List: 4 6 7 2 5 9
insertBefore(2, 3) List: 4 6 7 3 2 5 9
insertBefore(1, 8) IndexOutOfBoundsException List: 4 6 7 3 2 5 9
Problem 2
Implement sorting and duplicate removal operations on the linked list:
sortList(): sort the list in ascending order of the keys
Example:
List: 4 6 7 2 5 9
sortList() List: 2 4 5 6 7 9
removeDuplicates(): remove duplicate elements from the list.
Example:
List: 2 4 4 1 1 3 9
removeDuplicates() List: 2 4 1 3 9
Problem 3
Implement a mergeLists method in LinkedListTest class that merges 2 given lists and returns an output list. While merging make sure:
-Output list is sorted
-Remove any duplicate
Example: list1: 2 4 7 9
list2: 4 5 1 6 7 merge (list1, list2) Output: 1 2 4 5 6 7 9
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