// *********************************************************************** // // SortedSetNode Class Interface // // *********************************************************************** // Computer Science 102: Data Structures // New York University, Fall 2013, // // Lecturers: Eric Koskinen and Daniel Schwartz-Narbonne // // *********************************************************************** import java.lang.*; public class SortedSetNode implements Set { protected String value; protected SortedSetNode next; public SortedSetNode(String v, SortedSetNode n) { value = v; next = n; } public SortedSetNode(String v) { value = v; next = null; } public String getValue() { return value; } public SortedSetNode getNext() { return next; } // Basic operations public int size() { if(next == null) { //Base case, list with one element return 1; } else { //recursive case return 1 + next.size(); } } public String min() { //the head is the smallest node on a sorted list //so just return our own value return value; } public String max() { if(next == null){ //base case, last (largest) element on the list return value; } else { //recurse to find the largest element return next.max(); } } public boolean isEmpty() { //there is at least one node, i.e. the one we are currently visiting return false; } //Returns true if the list contains the element el //false otherwise public boolean contains(String el) { if(el == null){ return false; //null strings cannot be in the set } int c = value.compareTo(el); if (c == 0){ // value == el //we found it return true; } else if(c > 0){ // value > el //our current node is already greater than the target //the target is not in the list return false; } else { //current node is smaller than the target if (next == null) { //at the end of the list, so node not on list return false; } else { return next.contains(el); } } } //adds a node to the list, and then returns true if successful //if the node already existed, returns false public boolean add(String el) { //Sanitize our inputs if(el == null) { return false; } //Find the node we want to add after //There is an invarient that this node's value is always less than //or equal to the element's value if(el.equals(value)){ //element is already in the list return false; } else if(next == null) { //end of the list, add the element next = new SortedSetNode(el,next); return true; } else if (next.getValue().compareTo(el) > 0) { //we have the situation value < el < next.value //so add it after this node next = new SortedSetNode(el,next); return true; } else { return (next.add(el)); } } //removes a node from the list //Returns true if successful, false if unable to remove the element public boolean remove(String element) { //Sanitize our inputs if(element == null) { return false; } //find the node we want to remove after //there is an invarient that value != element int c = element.compareTo(value); if (c < 0) { //element is less than the current node value //hence element doesnt appear within the list //or else we would have found it by now return false; } else { //element is greater than the current node if(next == null){ //list ends, without finding element return false; } else if (next.getValue().equals(element)) { //we found the node to remove next = next.getNext(); return true; } else { //recursive case return next.remove(element); } } } //Converts the list to a string following the described format public String toString() { if (next == null) { return " \"" + value + "\" "; } else { return " \"" + value + "\" ;" + next.toString(); } } public void PrettyPrint() { System.out.println(this.toString()); } }