Question
In Java, create a linked list implementation of an OrderedSet class that contains a collection of unique objects. More formally, a set contains no pair
In Java, create a linked list implementation of an OrderedSet class that contains a collection of unique objects. More formally, a set contains no pair of elements e1 and e2 such that e1.compare(e2) == 0. Also, make sure that the set elements are always maintained in ascending order.
The set class should have the following methods:
- Set() -- Create an empty set
- void add(Item item) -- Add an item if the item does not exist in the set. if the item already exits, do nothing. Add should insert the element at the correct position and maintain the list in ascending order.
- boolean remove(Item item) -- Remove an item from the set. Return true if the item was removed and false if the item did not exist in the set.
- Item removeMin() -- Return the smallest item in the set. The item should be removed from the set.
- boolean isEmpty() -- Is the set empty? Return true or false.
- int size() -- Remove the number of items in the Set.
Use a singly linked list. Create an inner class Node that contains the fields item and next to support your OrderedSet class. You can assume that the Item class implements the compareTo method and returns the following values.
- if item1 < item2 then item1.compareTo(item2) returns < 0
- if item1 == item2 then item1.compareTo(item2) returns 0
- if item1 > item2 then item1.compareTo(item2) returns > 0
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