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");
}
SOLUTION:
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) {
Node node = new Node(element, null);
Node current = first;
// It will check if List is empty or not. If element will be stored
in the first and return true
if(first==null){
first = node;
return true;
}else{
// This while loop will check if the element already exist in the
list or not
// If exist it will return false and if not the element will be
added to the list and return true
while(current.next!=null){
// If condition for checking element exist in the list or not
if(current.data == element){
return false;
}
current = current.next;
}
// Boundary Value Check
if(current.data==element){
return false;
}else{
// IF element doesn't exist then element will be added
current.next = node;
}
}
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) {
Node current = first;
// This while loop will check if the element exist in the list or
not
// If exist it will remove the element and return true
while(current.next!=null){
if(current.next.data==element){
current = current.next.next;
return true;
}
current = current.next;
}
// condition when the element is present at the first position in
the list
if(first.data == element){
first = first.next;
return true;
}
return false;
}
}