Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. package sets;
  2.  
  3. import java.util.Iterator;
  4.  
  5. public class LinkedSet<E> implements Set<E> {
  6. private LinkedNode<E> head = null;
  7.  
  8. // Constructors
  9. public LinkedSet() {
  10. }
  11.  
  12. public LinkedSet(E e) {
  13. this.head = new LinkedNode<E>(e, null);
  14. }
  15.  
  16. private LinkedSet(LinkedNode<E> head) {
  17. this.head = head;
  18. }
  19.  
  20. // size : returns the number of elements in the set
  21. // consumes nothing and produces an int
  22. @Override
  23. public int size() {
  24. int iterations = 0;
  25. Iterator<E> iter = iterator() ;
  26. while (iter.hasNext()) {
  27. iterations++ ;
  28. }
  29. return iterations;
  30. }
  31.  
  32. // isEmpty() : Returns true if this set contains no elements.
  33. // consumes nothing and produces a boolean
  34. @Override
  35. public boolean isEmpty() {
  36. return (head == null) ;
  37. }
  38.  
  39. // iterator() : Returns an iterator over the elements in this set.
  40. // consumes nothing and produces an Iterator<E>
  41. @Override
  42. public Iterator<E> iterator() {
  43. return new LinkedNodeIterator<E>(this.head);
  44. }
  45.  
  46. // contains() : Returns true if this set contains the specified element.
  47. // consumes an object and produces a boolean
  48. @Override
  49. public boolean contains(Object o) {
  50. Iterator<E> iter = iterator() ;
  51. boolean answer = false ;
  52. while (iter.hasNext()) {
  53. if (iter.next().equals(o)) {
  54. answer = true ;
  55. }
  56. }
  57. return answer ;
  58. }
  59.  
  60. // isSubset() : Returns true if this set is a subset (or equal to) the given set.
  61. // consumes a Set<E> and produces a boolean
  62. @Override
  63. public boolean isSubset(Set<E> that) {
  64. boolean answer = true ;
  65. Iterator<E> iter = iterator();
  66. while (iter.hasNext()) {
  67. if (!that.contains(iter.next())) answer = false ;
  68. }
  69. return answer ;
  70. }
  71.  
  72. // isSuperset() : Returns true if this set is a superset (or equal to) the given set
  73. // consumes a Set<E> and produces a boolean
  74. @Override
  75. public boolean isSuperset(Set<E> that) {
  76. boolean answer = true ;
  77. return that.isSubset(this) ;
  78. }
  79.  
  80. // adjoin(): Returns a new Set containing all of the elements of this set and
  81. // also the specified new element. If the specified element is
  82. // already in the set then returns the original Set.
  83. // consumes an object E and produces a set
  84. @Override
  85. public Set<E> adjoin(E e) {
  86. LinkedSet<E> set = new LinkedSet(head) ;
  87. if (contains(e)) return set ;
  88. else {
  89. LinkedNode<E> addition = new LinkedNode(e, head) ;
  90. set = new LinkedSet(addition) ;
  91. return set ;
  92. }
  93. }
  94.  
  95. // union() : Returns a new <code>Set</code> that is the union of <code>this Set</code> and <code>that Set</code>.
  96. // consumes a set and produces a set
  97. @Override
  98. public Set<E> union(Set<E> that) {
  99. Iterator<E> iter = that.iterator() ;
  100. LinkedSet<E> set = new LinkedSet(this.head) ;
  101. while(iter.hasNext()) {
  102. set = (LinkedSet<E>) set.adjoin(iter.next()) ;
  103. }
  104. return set;
  105. }
  106.  
  107. // intersect() : Returns a new <code>Set</code> that is the intersection of <code>this Set</code> and <code>that Set</code>.
  108. // consumes a set and produces a set
  109. @Override
  110. public Set<E> intersect(Set<E> that) {
  111. Iterator<E> iter = iterator() ;
  112. LinkedSet<E> set = null ;
  113. if (isSubset(that)) {
  114. set = this ;
  115. }
  116. else if (isSuperset(that)) {
  117. set = (LinkedSet<E>) that ;
  118. }
  119. else {
  120. while (iter.hasNext()) {
  121. E temp = iter.next() ;
  122. if (that.contains(temp)) {
  123. if (set == null){
  124. set = new LinkedSet<E>(temp) ;
  125. }
  126. set = (LinkedSet<E>) set.adjoin(temp) ;
  127. }
  128. }
  129. }
  130. return set;
  131. }
  132.  
  133. // subtract() : Returns a new <code>Set</code> that is the difference of
  134. // <code>this Set</code> and <code>that Set</code>, i.e., <code>
  135. // this - that</code>.
  136. // consumes a set and produces a set
  137. @Override
  138. public Set<E> subtract(Set<E> that) {
  139. Iterator iter = iterator() ;
  140. LinkedSet<E> set = new LinkedSet<E>() ;
  141. if (!isSubset(that)) {
  142. while (iter.hasNext()) {
  143. E element = (E) iter.next() ;
  144. if (!that.contains(element)) {
  145. set = (LinkedSet<E>) set.adjoin(element) ;
  146. }
  147. }
  148. }
  149. return set ;
  150. }
  151.  
  152. // remove() : Returns a new <code>Set</code> that is the same as
  153. // <code>this</code> except that it does not contain the specified
  154. // element.
  155. // consumes an object e and produces a set
  156. @Override
  157. public Set<E> remove(E e) {
  158. Iterator iter = iterator() ;
  159. LinkedSet<E> set = null ;
  160. while (iter.hasNext()) {
  161. E temp = (E) iter.next();
  162. if (!temp.equals(e)) {
  163. if (set == null){
  164. set = new LinkedSet<E>(temp) ;
  165. }
  166. set = (LinkedSet<E>) set.adjoin(temp) ;
  167. }
  168. }
  169. return set;
  170. }
  171.  
  172. @Override
  173. @SuppressWarnings("unchecked")
  174. public boolean equals(Object o) {
  175. if (! (o instanceof Set)) {
  176. return false;
  177. }
  178. Set<E> that = (Set<E>)o;
  179. return this.isSubset(that) && that.isSubset(this);
  180. }
  181.  
  182. @Override
  183. public int hashCode() {
  184. int result = 0;
  185. for (E e : this) {
  186. result += e.hashCode();
  187. }
  188. return result;
  189. }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement