Hi everyone.
I wrote this interface : Dictionary<K, V> extends Iterable<K>
and this class : LinkedList<K, V> implements Dictionary<K,V> but i not have idea how to write iterator for this class. Someone can help me ?
thank you!
public interface Dictionary<K, V> extends Iterable<K> { public void insert(K key, V value); public void delete(K key); public V search(K key); }
public class LinkedList<K, V> implements Dictionary<K,V>{ Record list = null; class Record<K,V> { K key; V value; Record next; Record prev; Record(K key, V value){ this.key = key; this.value = value; } } @Override public Iterator<K> iterator() { // TODO Auto-generated method stub return null; } @Override public void insert(K key, V value) { Record nuovo = new Record(key, value); if(list==null){ list = nuovo.next = nuovo.prev = nuovo; }else{ nuovo.next = list.next; list.next.prev = nuovo; list.next = nuovo; nuovo.prev = list; } } @Override public void delete(K key) { Record p = null; if(list!=null) for(p=list.next;;p=p.next){ if(p.key.equals(key)) break; if(p==list){ p = null; break; } } if(p==null) throw new RuntimeException("element not found"); if(p.next == p) list = null; else { if(list==p) list = p.next; p.next.prev= p.prev; p.prev.next = p.next; } } @Override public V search(K key) { if(list==null) throw new RuntimeException("lista vuota"); for(Record p =list.next;;p=p.next){ if(p.key.equals(key)) return (V) p.value; if(p==list) return null; } }