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

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!