In: Computer Science
JAVA
the task is to implement the missing methods in the LinkedIntSet class. Each method you must write has comments describing how it should behave. You may not add any new fields to the LinkedIntSet class and you may only add new code inside the bodies of the 5 methods you are asked to write. You may also write new private helper methods. However, you may not change any method headers, you may not change any code outside of the 5 methods, and you may not add any new data fields to the class.
public class LinkedIntSet {
private static class Node {
private int data;
private Node next;
public Node(int data, Node next)
{
this.data =
data;
this.next =
next;
}
}
private Node first;
public LinkedIntSet() {
first = null;
}
public int size() {
int counter = 0;
for (Node current = first; current
!= null; current = current.next)
counter++;
return counter;
}
public boolean contains(int i) {
for (Node current = first; current
!= null; current = current.next) {
if (current.data
== i)
return true;
}
return false;
}
// Ignore this equals method. Write the code for
the other equals method.
public boolean equals(Object otherObject) {
LinkedIntSet other = (LinkedIntSet)
otherObject;
return this.equals(other);
}
/***************************** NEW METHODS ************************************/
/**
* Adds <code>element</code> to this set if
it is not already present and
* returns <code>true</code>. If
<code>element</code> is already present, the
* set is unchanged and <code>false</code>
is returned.
*
* @param element the element to be added
* @return <code>true</code> if the element
was added and <code>false</code>
* otherwise.
*/
public boolean addElement(int element) {
// Replace the line below with your
answer
throw new RuntimeException("Not
implemented");
}
/**
* Removes an element from the set.
*
* @param element the element to be removed
* @return <code>ture</code> if the element
was removed and <code>false</code>
* otherwise.
*/
public boolean removeElement(int element) {
// Replace the line below with your
answer
throw new RuntimeException("Not
implemented");
}
/**
* Changes the set so that it is equal the union of
itself and
* <code>other</code>.
*
* @param other the set to union with
*/
public void union(LinkedIntSet other) {
// Replace the line below with your
answer
throw new RuntimeException("Not
implemented");
}
/**
* Changes the set so that is equal the intersection of
itself and
* <code>other</code>.
*
* @param other the set to intersect with
*/
public void intersect(LinkedIntSet other) {
// Replace the line below with your
answer
throw new RuntimeException("Not
implemented");
}
}
Code
public class LinkedIntSet {
private static class Node {
private int data;
private Node next;
public Node(int data, Node next) {
this.data = data;
this.next = next;
}
}
private Node first;
public LinkedIntSet() {
first = null;
}
public int size() {
int counter = 0;
for (Node current = first; current != null; current =
current.next)
counter++;
return counter;
}
public boolean contains(int i) {
for (Node current = first; current != null; current = current.next)
{
if (current.data == i)
return true;
}
return false;
}
// Ignore this equals method. Write the code for the other
equals method.
public boolean equals(Object otherObject) {
LinkedIntSet other = (LinkedIntSet) otherObject;
//both set are equals when both same number of elements and same
elements not in exact same order
if(size()!=other.size())//if both the set has not same size return
false
return false;
//check this set's every element present in other set if so then
return true otherwise return fasle
for (Node current = first; current != null; current = current.next)
{
if(other.contains(current.data))
return false;
}
return true;
}
/***************************** NEW METHODS ************************************/
/**
* Adds <code>element</code> to this set if it is not
already present and
* returns <code>true</code>. If
<code>element</code> is already present, the
* set is unchanged and <code>false</code> is
returned.
*
* @param element the element to be added
* @return <code>true</code> if the element was added
and <code>false</code>
* otherwise.
*/
public boolean addElement(int element) {
//first check if the element present in the set if it is then just
retunr false
if(this.contains(element))
return false;
//creates the new node with element
Node newNode=new Node(element, null);
if(first==null)//check if first is nulll means set is empty then
add this new node at first
{
first=newNode;
return true;
}
Node curr=first;
while(curr.next!=null)//otherwise got to last node in the set
{
curr=curr.next;
}
//and then add new node to the last node
curr.next=newNode;
return true;
}
/**
* Removes an element from the set.
*
* @param element the element to be removed
* @return <code>ture</code> if the element was removed
and <code>false</code>
* otherwise.
*/
public boolean removeElement(int element) {
//first check element is present in the set or set is empty then
return flase
if(!this.contains(element) || first==null)
return false;
//checks if the first node has same element then remove first and
return true
if(first.data==element)
{
first=first.next;
return true;
}
//node curr and prev
Node curr=first;
Node prev=first;
//loops until curr.next is not null
while(curr.next!=null)
{
if(curr.data==element)//break the loop when
curr.data==element
break;
prev=curr;//assing curr to prev
curr=curr.next;//and take curr to one stpe ahead in set
}
if(curr.next==null)//this conditon true whnen element present at
the last node
prev.next=null;
else//else just simply skip the curr node
prev.next=curr.next;
return true;//return true
}
/**
* Changes the set so that it is equal the union of itself and
* <code>other</code>.
*
* @param other the set to union with
*/
public void union(LinkedIntSet other) {
//union means this set has all the element present in both the
set
//so simply loop through other set and add other each element to
this set
for (Node current = other.first; current != null; current =
current.next)
this.addElement(current.data);
}
/**
* Changes the set so that is equal the intersection of itself
and
* <code>other</code>.
*
* @param other the set to intersect with
*/
public void intersect(LinkedIntSet other) {
//intersect menas this set has only those elemts that are present
in both the set
//so loops through this set and remove the elemtn from the this set
which is not presetn in other set
//this set contains only elements that are presetn in booth the
set
for (Node current = first; current != null; current =
current.next)
{
if(!other.contains(current.data))
{
this.removeElement(current.data);
}
}
}
}
If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.