-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyset.java
More file actions
51 lines (51 loc) · 1.26 KB
/
Myset.java
File metadata and controls
51 lines (51 loc) · 1.26 KB
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
class Myset<X> {
public MyLinkedList set = new MyLinkedList();
public Boolean IsEmpty() { return set.isEmpty(); }
public Boolean IsMember(X o) {
Node<X> temp = set.head;
while(temp!=null) {
if(temp.data==o) return true;
temp = temp.next;
}
return false;
}
public void Insert(X o) { if(!IsMember(o)) set.addLast(o); }
public Myset Union(Myset a) {
Myset temp = new Myset();
MyLinkedList temp2 = this.set;
MyLinkedList templ = a.set;
X tempo;
while(!templ.isEmpty()) {
tempo = (X)templ.head.data;
if(!IsMember(tempo)) { temp2.addFirst(tempo); }
templ.removeFirst();
}
temp.set = temp2;
return temp;
}
public Myset Intersection(Myset a) {
Myset temp = new Myset();
MyLinkedList temp2 = new MyLinkedList();
MyLinkedList templ = a.set;
X tempo;
while(!templ.isEmpty()) {
tempo = (X)templ.head.data;
if(IsMember(tempo)) { temp2.addFirst(tempo); }
templ.removeFirst();
}
temp.set = temp2;
return temp;
}
public void Delete(X o) throws Exception {
if(IsMember(o)) {
if(set.head.data==o) {
set.head = set.head.next;
}
else {
Node<X> temp = set.head;
while(temp.next.data!=o) { temp = temp.next; }
temp.next = temp.next.next;
}
} else { throw new Exception("Delete: X not found!"); }
}
}