Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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> implements Set { ////////////////////////////////////////////////////////// // Do not change the following three fields in any way. // ////////////////////////////////////////////////////////// /** References to the first and last node of the list. */ Node front; Node rear; /** The number of nodes in the list. */ int size; ///////////////////////////////////////////////////////// // Do not change the following constructor in any way. // ///////////////////////////////////////////////////////// /** * Instantiates an empty LinkedSet. */ public LinkedSet() { front = null; rear = null; size = 0; } ////////////////////////////////////////////////// // Public interface and class-specific methods. // ////////////////////////////////////////////////// /////////////////////////////////////// // DO NOT CHANGE THE TOSTRING METHOD // /////////////////////////////////////// /** * Return a string representation of this LinkedSet. * * @return a string representation of this LinkedSet */ @Override public String toString() { if (isEmpty()) { return "[]"; } StringBuilder result = new StringBuilder(); result.append("["); for (T element : this) { result.append(element + ", "); } result.delete(result.length() - 2, result.length()); result.append("]"); return result.toString(); } /////////////////////////////////// // DO NOT CHANGE THE SIZE METHOD // /////////////////////////////////// /** * Returns the current size of this collection. * * @return the number of elements in this collection. */ public int size() { return size; } ////////////////////////////////////// // DO NOT CHANGE THE ISEMPTY METHOD // //////////////////////////////////////

/** * 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David J. Auer David M. Kroenke

13th Edition

B01366W6DS, 978-0133058352

More Books

Students also viewed these Databases questions