Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package TDA_Mapeo;
- import TDA_Lista.BoundaryViolationException;
- import TDA_Lista.DoubleLinkedList;
- import TDA_Lista.EmptyListException;
- import TDA_Lista.InvalidPositionException;
- import TDA_Lista.ListaSimplementeEnlazada;
- import TDA_Lista.Position;
- import TDA_Lista.PositionList;
- import ColaConPrioridad.Entrada;
- import ColaConPrioridad.Entry;
- public class MapeoConLista<K, V> implements Map<K, V> {
- protected DoubleLinkedList<Entry<K,V>> S;
- public int size() {
- return S.size();
- }
- public boolean isEmpty() {
- return S.isEmpty();
- }
- public V get(K k) {
- Position<Entry<K,V>> nuevo=null;
- try{
- if(!S.isEmpty()){
- nuevo=S.first();
- while(nuevo!=null&&nuevo.element().getKey()!=k){
- if(nuevo==S.last())
- nuevo=null;
- else
- nuevo=S.next(nuevo);
- }
- }
- }catch(EmptyListException e){
- System.out.println(e.getMessage());
- }catch(InvalidPositionException e){
- System.out.println(e.getMessage());
- }catch(BoundaryViolationException e){
- System.out.println(e.getMessage());}
- if (nuevo!=null&&nuevo.element().getKey() == k)
- return nuevo.element().getValue();
- else
- return null;
- }
- public V put(K k, V v) {
- Entry<K,V> nuevo=new Entrada<K,V>(k,v);
- if(S.isEmpty())
- S.addFirst(nuevo);
- else{
- Position<Entry<K,V>> cursor=S.first();
- boolean encontre=false;
- while(cursor!=null&&!encontre){
- if(cursor.element().getKey()==k){
- S.set(cursor, nuevo);
- encontre=true;
- cursor=null;
- }
- }
- if(!encontre)
- S.addLast(nuevo);
- }
- return null;
- }
- public V remove(K k) {
- V valor=null;
- if(!S.isEmpty()){
- Position<Entry<K,V>> cursor=S.first();
- boolean encontre=false;
- while(!encontre&&cursor!=null){
- encontre=cursor.element().getKey()==k;
- if(encontre)
- valor=cursor.element().getValue();
- S.remove(cursor);
- if(cursor==S.last())
- cursor=null;
- else
- cursor=S.next(cursor);
- }
- }
- return valor;
- }
- public Iterable<K> keys() {
- PositionList<K>lista= new DoubleLinkedList<K>();
- if(!S.isEmpty())
- for(Entry<K,V> h: S){
- lista.addLast(h.getKey());
- }
- return lista;
- }
- public Iterable<V> values() {
- PositionList<V>lista= new DoubleLinkedList<V>();
- if(!S.isEmpty())
- for(Entry<K,V> h: S){
- lista.addLast(h.getValue());
- }
- return lista;
- }
- public Iterable<Entry<K, V>> entries() {
- PositionList<Entry<K,V>> lista=new DoubleLinkedList<Entry<K,V>>();
- if(!S.isEmpty())
- for(Entry<K,V> h: S){
- lista.addLast(h);
- }
- return lista;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement