Join for FREE | Take the Tour Lost Password?
[x]

deviantART

 
About Me Member Fractal Artist zuyx17/Male/United States Recent Activity Deviant for 1 Year
Needs Premium Membership
Statistics 224 Deviations
3,393 Comments
3,816 Pageviews

Hash Set C:

Fri Nov 6, 2009, 7:58 PM
Clearing my desktop, had Hash Set from Computer Science 4 on there.
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;

}

}

  • Mood: Yearning
  • Watching: The time go by before I'm next to her.
  • Drinking: Air

Journal History

Devious Info

  • Favourite movie: The Last Unicorn
  • Favourite band or musician: Pink Floyd and Ferry Corsten
  • Favourite genre of music: Trance
  • Favourite artist: Ferry Corsten
  • Favourite style of art: Abstract
  • Operating System: Linux
  • Shell of choice: Ubuntu
  • Favourite game: Final Fantasy VI - Opera Scene is best in video game history
  • Favourite gaming platform: PSP
  • Tools of the Trade: Paint.Net Chaoscope Apophysis

deviantID

I'm Doug.

Think of me however you want to,
though I hope it's for the better.

I love this one girl, ~Sarah-Annabell

deviantART Community Board

[x]

Comments


:iconverysmallindeed:
:granny: GRANDMOTHER!

--
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]
:iconzuyx:
....
What?!

--
In my opinion, we don't devote nearly enough scientific research to finding a cure for jerks.
:iconzuyx:
No problem

--
In my opinion, we don't devote nearly enough scientific research to finding a cure for jerks.
:iconamericancrimesociety:
Hi!

Thanks for the watch!

--
Dont try me at home.

Site Map