-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyset.java
More file actions
206 lines (175 loc) · 4.31 KB
/
Myset.java
File metadata and controls
206 lines (175 loc) · 4.31 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import java.util.*;
class LinkedList
{
Node head;
Node tail;
public class Node
{
Object data;
Node next;
Node(Object d)
{
data = d;
next = null;
}
}// head of list
/* Linked list Node*/
public int getCount()
{
Node temp = head;
int count = 0;
while (temp != null)
{
count++;
temp = temp.next;
}
return count;
}
/* Function to get Union of 2 Linked Lists */
void getUnion(Node head1, Node head2)
{
Node t1 = head1, t2 = head2;
//insert all elements of list1 in the result
while (t1 != null)
{
pushh(t1.data);
t1 = t1.next;
}
// insert those elements of list2 that are not present
while (t2 != null)
{
if (!isPresent(head, t2.data))
pushh(t2.data);
t2 = t2.next;
}
}
void getIntersection(Node head1, Node head2)
{
Node t1 = head1;
// Traverse list1 and search each element of it in list2.
// If the element is present in list 2, then insert the
// element to result
while (t1 != null)
{
if (isPresent(head2, t1.data))
pushh(t1.data);
t1 = t1.next;
}
}
/* Utility function to print list */
void printList()
{
Node temp = head;
while(temp != null)
{
System.out.print(temp.data+" ");
temp = temp.next;
}
System.out.println();
}
void rearpush(Object new_data)
{
}
/* Inserts a node at start of linked list */
void pushh(Object new_data)
{
/* 1 & 2: Allocate the Node &
Put in the data*/
Node new_node = new Node(new_data);
/* 3. Make next of new Node as head */
new_node.next = head;
/* 4. Move the head to point to new Node */
head = new_node;
}
void deleteNode(Object key) throws Exception
{
Node temp = head, prev = null;
if (temp != null && temp.data == key)
{
head = temp.next;
return;
}
while (temp != null && temp.data != key)
{
prev = temp;
temp = temp.next;
}
if (temp == null) throw new Exception();
prev.next = temp.next;
}
public Node returnithnode(int a)
{
Node temp=head;
int c=this.getCount();
for(int i=(c-1);i>(c-a);i--)
{
temp=temp.next;
}
return temp;
}
/* A utilty function that returns true if data is present
in linked list else return false */
boolean isPresent (Node head, Object data)
{
Node t = head;
while (t != null)
{
if (t.data == data)
return true;
t = t.next;
}
return false;
}
public boolean isEmpty() {
if (head == null)
return true;
else
return false;
}
}
public class Myset
{
LinkedList ll=new LinkedList();
public boolean IsEmpty()
{
return ll.isEmpty();
}
public boolean IsMember(Object d)
{
return ll.isPresent(ll.head,d);
}
public void Insert(Object d)
{
if(!IsMember(d))
this.ll.pushh(d);
}
public void Delete(Object d)
{
try {
ll.deleteNode(d);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Myset Union(Myset a)
{
Myset c=new Myset();
c.ll.getUnion(this.ll.head,a.ll.head);
return c;
}
public Myset Intersection(Myset a)
{
Myset d=new Myset();
d.ll.getIntersection(this.ll.head,a.ll.head);
return d;
}
public int getcount()
{
return ll.getCount();
}
public Object getith(int a)
{
return ll.returnithnode(a).data;
}
}