Skip to content

Instantly share code, notes, and snippets.

@rpcarney4
Last active October 20, 2018 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rpcarney4/c50dd44d2728c266b26b621bebb12444 to your computer and use it in GitHub Desktop.
Save rpcarney4/c50dd44d2728c266b26b621bebb12444 to your computer and use it in GitHub Desktop.
Auburn University-COMP 2210 LinkedSet Assignment.
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 Dean Hendrix (dh@auburn.edu)
* @author Ryan Carney (rpc0016@auburn.edu)
* @version 10/18/18
*
*/
public class LinkedSet<T extends Comparable<? super T>> implements Set<T> {
//////////////////////////////////////////////////////////
// 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 || this.contains(element)) {
return false;
}
Node a = new Node(element);
a.next = front;
front = a;
size++;
return true;
}
/**
* Ensures the collection does not contain the specified element.
* If the specified element is present, this method removes it
* from the collection. This method, consistent with add, ensures
* that the elements in the linked lists are maintained in ascending
* natural order.
*
* @param element The element to be removed.
* @return true if collection is changed, false otherwise.
*/
public boolean remove(T element) {
Node a = find(element);
if (a == null) {
return false;
}
if (size == 1) {
front = null;
size = 0;
return true;
}
if (a == front) {
front = front.next;
front.prev = null;
}
else {
a.prev.next = a.next;
if (a.next != null) {
a.next.prev = a.prev;
}
}
size--;
return true;
}
/**
* Searches for specified element in this collection.
*
* @param element The element whose presence in this collection is to be tested.
* @return true if this collection contains the specified element, false otherwise.
*/
public boolean contains(T element) {
return find(element) != null;
}
/**
* Tests for equality between this set and the parameter set.
* Returns true if this set contains exactly the same elements
* as the parameter set, regardless of order.
*
* @return true if this set contains exactly the same elements as
* the parameter set, false otherwise
*/
public boolean equals(Set<T> s) {
if (s.size() != this.size()) {
return false;
}
}
/**
* Tests for equality between this set and the parameter set.
* Returns true if this set contains exactly the same elements
* as the parameter set, regardless of order.
*
* @return true if this set contains exactly the same elements as
* the parameter set, false otherwise
*/
public boolean equals(LinkedSet<T> s) {
boolean output = false;
if (s.size() != this.size()) {
return false;
}
}
/**
* Returns a set that is the union of this set and the parameter set.
*
* @return a set that contains all the elements of this set and the parameter set
*/
public Set<T> union(Set<T> s) {
if (s.isEmpty() || this.equals(s)) {
return this;
}
else if (this.isEmpty()) {
return s;
}
else {
Set<T> uSet = new LinkedSet<T>();
for (T element1 : s) {
uSet.add(element1);
}
for (T element2 : s) {
uSet.add(element2);
}
return uSet;
}
}
/**
* Returns a set that is the union of this set and the parameter set.
*
* @return a set that contains all the elements of this set and the parameter set
*/
public Set<T> union(LinkedSet<T> s) {
if (s.isEmpty() || this.equals(s)) {
return this;
}
else if (this.isEmpty()) {
return s;
}
else {
Set<T> uSet = new LinkedSet<T>();
for (T element1 : s) {
uSet.add(element1);
}
for (T element2 : s) {
uSet.add(element2);
}
return uSet;
}
}
/**
* Returns a set that is the intersection of this set and the parameter set.
*
* @return a set that contains elements that are in both
* this set and the parameter set
*/
public Set<T> intersection(Set<T> s) {
if (this.equals(s) || this.isEmpty()) {
return this;
}
else if (s.isEmpty()) {
return s;
}
else {
LinkedSet<T> iSet = new LinkedSet<T>();
for (T element1 : s) {
if (this.contains(element1)) {
iSet.add(element1);
}
}
return iSet;
}
}
/**
* Returns a set that is the intersection of this set and
* the parameter set.
*
* @return a set that contains elements that are in both
* this set and the parameter set
*/
public Set<T> intersection(LinkedSet<T> s) {
if (this.equals(s) || this.isEmpty()) {
return this;
}
else if (s.isEmpty()) {
return s;
}
else {
LinkedSet<T> iSet = new LinkedSet<T>();
for (T element1 : s) {
if (this.contains(element1)) {
iSet.add(element1);
}
}
return iSet;
}
}
/**
* Returns a set that is the complement of this set and the parameter set.
*
* @return a set that contains elements that are in this set but not the parameter set
*/
public Set<T> complement(Set<T> s) {
Set<T> cSet = new LinkedSet<T>();
if (this.equals(s)) {
return cSet;
}
else if (this.isEmpty() || s.isEmpty()) {
return this;
}
else {
for (T element1 : s) {
this.remove(element1);
}
for (int i = size() - 1; i >= 0; i--) {
}
}
}
/**
* Returns a set that is the complement of this set and
* the parameter set.
*
* @return a set that contains elements that are in this
* set but not the parameter set
*/
public Set<T> complement(LinkedSet<T> s) {
return null;
}
/**
* Returns an iterator over the elements in this LinkedSet.
* Elements are returned in ascending natural order.
*
* @return an iterator over the elements in this LinkedSet
*/
public Iterator<T> iterator() {
return new LinkedIterator();
}
/**
* Returns an iterator over the elements in this LinkedSet.
* Elements are returned in descending natural order.
*
* @return an iterator over the elements in this LinkedSet
*/
public Iterator<T> descendingIterator() {
return new DescendingIterator();
}
/**
* Returns an iterator over the members of the power set
* of this LinkedSet. No specific order can be assumed.
*
* @return an iterator over members of the power set
*/
public Iterator<Set<T>> powerSetIterator() {
return new PowerIterator();
}
//////////////////////////////
// Private utility methods. //
//////////////////////////////
// Feel free to add as many private methods as you need.
private Node find(T element) {
Node a = front;
while (a != null) {
if (a.element.equals(element)) {
return a;
a = a.next;
}
}
return null;
}
////////////////////
// Nested classes //
////////////////////
private class DescendingIterator implements Iterator<T> {
private Node on = front;
}
private class LinkedIterator implements Iterator<T> {
private Node on = front;
public boolean hasNext() {
return on != null;
}
public void remove() {
throw new UnsupportedOperationException();
}
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
T result = on.element;
on = on.next;
return result;
}
}
private class PowerIterator implements Iterator<T> {
}
//////////////////////////////////////////////
// DO NOT CHANGE THE NODE CLASS IN ANY WAY. //
//////////////////////////////////////////////
/**
* Defines a node class for a doubly-linked list.
*/
class Node {
/** the value stored in this node. */
T element;
/** a reference to the node after this node. */
Node next;
/** a reference to the node before this node. */
Node prev;
/**
* Instantiate an empty node.
*/
public Node() {
element = null;
next = null;
prev = null;
}
/**
* Instantiate a node that containts element
* and with no node before or after it.
*/
public Node(T e) {
element = e;
next = null;
prev = null;
}
}
}
import java.util.Iterator;
/**
* A collection that implements set behavior.
*
* @author Dean Hendrix (dh@auburn.edu)
* @version 2018-10-04
*
*/
public interface Set<T> extends Iterable<T> {
/**
* Ensures the collection contains the specified element.
* No specific order can be assumed. Neither duplicate nor null
* values are allowed.
*
* @param element The element whose presence is to be ensured.
* @return true if collection is changed, false otherwise.
*/
boolean add(T element);
/**
* Ensures the collection does not contain the specified element.
* If the specified element is present, this method removes it
* from the collection.
*
* @param element The element to be removed.
* @return true if collection is changed, false otherwise.
*/
boolean remove(T element);
/**
* Searches for specified element in this collection.
*
* @param element The element whose presence in this collection is to be tested.
* @return true if this collection contains the specified element, false otherwise.
*/
boolean contains(T element);
/**
* Returns the current size of this collection.
*
* @return the number of elements in this collection.
*/
int size();
/**
* Tests to see if this collection is empty.
*
* @return true if this collection contains no elements, false otherwise.
*/
boolean isEmpty();
/**
* Tests for equality between this set and the parameter set.
* Returns true if this set contains exactly the same elements
* as the parameter set, regardless of order.
*
* @return true if this set contains exactly the same elements as the parameter set, false otherwise
*/
boolean equals(Set<T> s);
/**
* Returns a set that is the union of this set and the parameter set.
*
* @return a set that contains all the elements of this set and the parameter set
*/
Set<T> union(Set<T> s);
/**
* Returns a set that is the intersection of this set and the parameter set.
*
* @return a set that contains elements that are in both this set and the parameter set
*/
Set<T> intersection(Set<T> s);
/**
* Returns a set that is the complement of this set and the parameter set.
*
* @return a set that contains elements that are in this set but not the parameter set
*/
Set<T> complement(Set<T> s);
/**
* Returns an iterator over the elements in this collection.
* No specific order can be assumed.
*
* @return an iterator over the elements in this collection
*/
Iterator<T> iterator();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment