Question
I need to ensure my add(T element) method maintains natural ascending order and I can't figure out how import java.util.Iterator; import java.util.NoSuchElementException; /** * Provides
I need to ensure my add(T element) method maintains natural ascending order and I can't figure out how
import java.util.Iterator;
import java.util.NoSuchElementException; /** * Provides an implementation of the Set interface. * A doubly-linked list is used as the underlying data structure. * Although not required by the interface, this linked list is * maintained in ascending natural order. In those methods that * take a LinkedSet as a parameter, this order is used to increase * efficiency. * * @author Kyle Schopper (kjs0031@auburn.edu) * @version 2016-03-13 * */ public class LinkedSet
/** * Tests to see if this collection is empty. * * @return true if this collection contains no elements, false otherwise. */ public boolean isEmpty() { return (size == 0); } /** * Ensures the collection contains the specified element. Neither duplicate * nor null values are allowed. This method ensures that the elements in the * linked list are maintained in ascending natural order. * * @param element The element whose presence is to be ensured. * @return true if collection is changed, false otherwise. */ public boolean add(T element) { if (element == null) { throw new NoSuchElementException(); } // Checks for duplicates. if (contains(element)) {
return false; } // Adds at position one of LinkedSet is empty. if (size == 0) { front = new Node(element); rear = front
}
// Adds to end of LinkedSet. else { rear.next = new Node(element); rear.next.prev = rear; rear = rear.next; } size++;
return true; }
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