/* Set.java */ import list.*; /** * A Set is a collection of Comparable elements stored in sorted order. * Duplicate elements are not permitted in a Set. **/ public class Set { /* Fill in the data fields here. */ /** * Set invariants: * 1) The Set's elements must be precisely the elements of the DList. * 2) setList must always contain Comparable elements, and those elements * must always be sorted in ascending order. * 3) No two elements in setList may be equals(). **/ /** * Constructs an empty Set. * * Performance: runs in O(1) time. **/ public Set() { // Your solution here. } /** * cardinality() returns the number of elements in this Set. * * Performance: runs in O(1) time. **/ public int cardinality() { // Replace the following line with your solution. return 0; } /** * insert() inserts a Comparable element into this Set. * * Sets are maintained in sorted order. The ordering is specified by the * compareTo() method of the java.lang.Comparable interface. * * Performance: runs in O(this.cardinality()) time. **/ public void insert(Comparable c) { // Your solution here. } /** * union() modifies this Set so that it contains all the elements it * started with, plus all the elements of s. The Set s is NOT modified. * Make sure that duplicate elements are not created. * * Performance: Must run in O(this.cardinality() + s.cardinality()). * * Your implementation should NOT copy elements or list nodes of "this", * and should not attempt to copy elements of s. However, it should create * new nodes for the elements that are shared with s. **/ public void union(Set s) { // Your solution here. } /** * intersect() modifies this Set so that it contains the intersection of * its own elements and the elements of s. The Set s is not modified. * * Performance: Must run in O(this.cardinality() + s.cardinality()). * * Do not construct any new DListNodes during the execution of intersect. * Reuse the nodes of "this" that will be in the intersection result. **/ public void intersect(Set s) { // Your solution here. } /** * toString() returns a String representation of this Set. The String must * have the following format: * { } for an empty Set. No spaces before "{" or after "}"; two spaces * between them. * { 1 2 3 } for a Set of three Integer elements. No spaces before * "{" or after "}"; two spaces before and after each element. * Elements are printed with their own toString method, whatever * that may be. The elements must appear in sorted order, from * lowest to highest according to the compareTo() method. **/ public String toString() { // Replace the following line with your solution. return ""; } public static void main(String[] argv) { Set s = new Set(); s.insert(new Integer(3)); s.insert(new Integer(4)); s.insert(new Integer(3)); System.out.println("Set s = " + s); Set s2 = new Set(); s2.insert(new Integer(4)); s2.insert(new Integer(5)); s2.insert(new Integer(5)); System.out.println("Set s2 = " + s2); Set s3 = new Set(); s3.insert(new Integer(5)); s3.insert(new Integer(3)); s3.insert(new Integer(8)); System.out.println("Set s3 = " + s3); s.union(s2); System.out.println("After s.union(s2), s = " + s); s.intersect(s3); System.out.println("After s.intersect(s3), s = " + s); System.out.println("s.cardinality() = " + s.cardinality()); // You may want to add more (ungraded) test code here. } }