-
Notifications
You must be signed in to change notification settings - Fork 0
/
MySet.java
62 lines (55 loc) · 1.09 KB
/
MySet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public class MySet<X>
{
private MyLinkedList<X> set;
public MySet()
{
set = new MyLinkedList<X>();
}
public boolean IsMember(X o)
{
return set.IsMember(o);
}
public void addElement(X element)
{
if(set.IsMember(element))
throw new RuntimeException("Error - Object is already in the set");
set.Insert(element);
}
public MySet<X> union(MySet<X> otherSet)
{
MySet<X> union = new MySet<X>();
MyLinkedList<X>.Node temp = set.head;
while(temp != null)
{
union.addElement(temp.obj);
temp = temp.next;
}
temp = otherSet.getElements().head;
while(temp != null)
{
try { union.addElement(temp.obj); }
catch(RuntimeException ex) {}
temp = temp.next;
}
return union;
}
public MySet<X> intersection(MySet<X> otherSet)
{
MySet<X> intersection = new MySet<X>();
MyLinkedList<X>.Node temp = set.head;
while(temp != null)
{
if(otherSet.IsMember(temp.obj)) intersection.addElement(temp.obj);
temp = temp.next;
}
return intersection;
}
public MyLinkedList<X> getElements()
{
return set;
}
public int size()
{
return set.size();
}
}