non-academic guidance for computer science

Information

This article was written on 17 Lis 2011, and is filled under ČVUT, edit (nedokonceno), Java, oop, Tutoring.

Generic SET


Abstrakt

Použití například

Set<String> set = new GenericSet<String>() ;

for ( String T : set )
  System.out.println ( T ) ;

Dokumentace & Licence

 

Kód

class GenericSet<P> implements java.util.Set<P>,Iterable<P>
{

/////////////////////////////////////////////////////////////////////////////

  private Node head ;
  private int count ;

/////////////////////////////////////////////////////////////////////////////

  public GenericSet()
  {
    clear();
    this.count = 0;
  }

/////////////////////////////////////////////////////////////////////////////

  private int size()
  {
    return this.count;
  }

/////////////////////////////////////////////////////////////////////////////

  public boolean add(P o)
  {
    Node temp = new Node(o);
    Node current = head;
    while(current.getNext() != null)
      current = current.getNext();

    current.setNext(temp);
    this.count++;
    return true;
  }

/////////////////////////////////////////////////////////////////////////////

  public boolean remove ( Object o )
  {
    Node current = head;
    while(current.getNext() != null && current.getNext().getData()!=o)
      current = current.getNext();
    current.setNext(current.getNext().getNext());
    this.count--;
    return true;
  }

/////////////////////////////////////////////////////////////////////////////

  public boolean addAll(java.util.Collection<? extends P> c) { return false; }

/////////////////////////////////////////////////////////////////////////////

  public void clear()
  {
    head = new Node(null);
  }

/////////////////////////////////////////////////////////////////////////////

  public boolean contains(Object o)
  {
    for(P n : this) if(n.equals(o)) return true;    
    return false;
  }

/////////////////////////////////////////////////////////////////////////////

  public boolean containsAll(java.util.Collection<?> c) { return false; }

/////////////////////////////////////////////////////////////////////////////

  public boolean isEmpty() { return this.size()==0; }

/////////////////////////////////////////////////////////////////////////////

  public java.util.Iterator<P> iterator() { return new GenericIterator(); }

/////////////////////////////////////////////////////////////////////////////

  public boolean removeAll(java.util.Collection<?> c) { return false; }

/////////////////////////////////////////////////////////////////////////////

  public boolean retainAll(java.util.Collection<?> c) { return false; }

/////////////////////////////////////////////////////////////////////////////

  public Object[] toArray() { return null; }

/////////////////////////////////////////////////////////////////////////////

  public <T> T[] toArray(T[] a) { return null; }

/////////////////////////////////////////////////////////////////////////////

  private class GenericIterator implements java.util.Iterator<P>
  {
    Node cur = head;
    public boolean hasNext() { return cur.next!=null; }
    public P next() { return (cur=cur.next).getData(); }
    public void remove() { }
  }

/////////////////////////////////////////////////////////////////////////////

  private class Node
  {
    Node next;
    P data;
    public Node(P _data) { next = null; data = _data; }
    public P getData() { return data; }
    public Node getNext() { return next; }
    public void setNext(Node _next) { next = _next; }
  }

/////////////////////////////////////////////////////////////////////////////

}

 

Download

Napsat komentář