Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

java help: /* * implements a hash table using chains of nodes for the buckets */ public class HashSet implements Set { private class Node

java help:

image text in transcribed

 
/*  * implements a hash table using chains of nodes for the buckets  */ public class HashSet implements Set { private class Node { private T data; private Node next; public Node(T item) { data = item; next = null; } public String toString() { return "" + data; } } public static final int DEFAULT_CAPACITY = 9; private Node[] hashtable; private int size; private int items; public HashSet () { this(DEFAULT_CAPACITY); } @SuppressWarnings("unchecked") public HashSet (int capacity) { hashtable = (Node[]) new HashSet.Node[capacity]; size = capacity; items = 0; } public boolean add (T item) { checkForNull(item); int index = getIndex(item); Node current = hashtable[index]; if (current == null) { hashtable[index] = new Node(item); items++; return true; } while (current.next!= null) { if (current.data.equals(item)) { return false; } current = current.next; } if (current.data.equals(item)) { return false; } current.next = new Node(item); items++; return true; } public boolean remove (T item) { // to be implemented return false; } public T remove () { return null; } public T get() { boolean filled = false; int random = 0; while (!filled) { random = (int)(Math.random() * items); if (hashtable[random] != null) { filled = true; } } return hashtable[random].data; } public boolean contains(T item) { int index = getIndex(item); Node current = hashtable[index]; if (current == null) { return false; } while (current.next != null) { if (current.data.equals(item)) { return true; } current = current.next; } if (current.data.equals(item)) { return true; } return false; } public int size() { return size; } public String toString() { if (size == 0) { return "[]"; } String h = ""; for (int i = 0; i  

 /*  * interface for a set  * * unordered collection, no duplicates allowed */ public interface Set { /* * adds item to set if not already present * * returns true if item successfully added, false if not * */ boolean add (T item); T remove(); boolean remove (T item); T get(); boolean contains (T item); int size(); String toString(); } 

/* * driver program for testing LinkedHashSet */  public class HashSets { public static void main (String [] args) { Set letters = new HashSet(); char [] toAdd = {'C', 'b', 'c', 'Q', 'z', 'D', 'E', 'P', 'j', 'F', 'E', 'I', 'y', 'f', 'i', 'U', 'V', 'm', 'e', 'W'}; for (int i = 0; i   In class we implemented a hash table for a set, with the buckets consisting of linked chains of nodes. Add the following method to the file: boolean remove (T item); Then test your code using HashSets. The output should look like this: 0 CQ 1 Im 2 3 6 Eiw 7 F 8 b P Does the set contain 'e'? yes Does the set contain 'a'? no After removing e, c, y, W, and y: 0 Q 1 Im 3 4 CU 5 ZDV 6 Ei 7 F 8 b P

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning C# 2005 Databases

Authors: Karli Watson

1st Edition

0470044063, 978-0470044063

More Books

Students also viewed these Databases questions