Haven't updated so here you can see.
*It doesn't look all pretty and tabbed out on DA ): *
import java.util.*;
public class Hashset extends AbstractSet{
public HashSet(int bucketsLength){
buckets = new Node[bucketsLength];
size = 0;
}
public boolean contains(Object x){
int h = x.hashCode();
if (h < 0) h = -h;
h = h % buckets.length;
Node current = buckets[h];
while (current != null){
if (current.data.equals(x)) return true;
current = current.next;
}
return false;
}
public boolean add(Object x){
int h = x.hashCode();
if (h < 0) h = -h;
h = h % buckets.length;
Node current = buckets[h];
while (current != null){
if (current.data.equals(x))
return false;
current = current.nextl
}
Node newNode = new Node();
newNode.data = x;
newNode.next = buckets[h];
buckets[h] = newNode;
size++;
return true;
}
public boolean remove(Object x){
int h = x.hashCode();
if (h < 0) h = -h;
h = h % buckets.length;
Node current = buckets[h];
Node previous = null;
while (current != null){
if (current.data.equals(x)){
if (previous == null) buckets[h] = current.next;
else previous.next = current.next;
size--;
return true;
}
previous = current;
current = current.next;
}
return false;
}
public Iterator iterator(){
return new HashSetIterator();
}
public int size(){
return size;
}
private Node[] buckets;
private int size;
private class Node{
public Object data;
public Node next;
}
private class HashSetIterator implements Iterator{
public HashSetIterator() {
current = null;
buckets = -1;
previous = null;
previousBucket = -1;
}
public boolean hasNext(){
if (current != null && current.next != null)
return true;
for (int b = bucket + 1; b < buckets.length; b++)
if (buckets[b] != null) return true;
return false;
}
public Object next() {
previous = current;
previousBucket = bucket;
if (current == null || current.next == null){
bucket++
while (bucket < buckets.length && buckets[bucket] ==null)
bucket++;
if (bucket < buckets.length)
current = buckets[bucket];
else
throw new NoSuchElementException();
}
else
current = current.next;
return current.data;
}
public void remove(){
if (previous != null && previous.next == current)
previous.next = current.next;
else if (previousBucket < bucket)
buckets[bucket] = current.next;
else
throw new IllegalStateException();
current = previous;
bucket = previousBucket;
}
private int bucket;
private Node current;
private int previousBucket;
private Node previous;
}
}










--
Behold one of nature's biggest mistakes, The Fly: small, fast, annoying and exceedingly fun to annihilate with a swatter! Ah ha! *SPLAT!*
Sooo...felt-tip sketches anyone? [link]
What?!
--
In my opinion, we don't devote nearly enough scientific research to finding a cure for jerks.
--
In my opinion, we don't devote nearly enough scientific research to finding a cure for jerks.
Thanks for the watch!
--
Dont try me at home.
Previous Page12345...Next Page