Create a linked list implementation of a Set class that contains a unique collec
ID: 3841853 • Letter: C
Question
Create a linked list implementation of a Set class that contains a unique collection of objects. More at formally, a set list that true, and contains no pair of elements e1 and e2 such e1. equals (e2) == true, and most one null element. The Set class should have the following methods. Use a singly linked list. Create an inner class Node that contains the fields item and next to support Create your Set class. Complete the get, put, size, and maxHeight methods for the following Binary Search Tree. The height of a node is the number of edges on the longest parh from the node to a leaf. A leaf node will have a height of 0.Explanation / Answer
public class Set {
Node start;
int size;
class Node{
Item item;
Node next;
Node(Item it, Node n)
{
item = it;
next = n;
}
}
public Set()
{
start=null;
size = 0;
}
public void add(Item item)
{
Node p = start;
while(p!=null)
{
if(p.item.equals(item))
return;
p = p.next;
}
start = new Node(item, start);
size++;
}
public boolean remove(Item item)
{
Node p = start, prev=null;
while(p!=null)
{
if(p.item.equals(item))
{
if(p == start)
{
start = p.next;
}
else
{
prev.next = p.next;
}
size--;
return true;
}
p = p.next;
}
return false;
}
public boolean isEmpty()
{
return size == 0;
}
public int size()
{
return size;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.