Advertisement
Guest User

Untitled

a guest
May 24th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. package TDA_Mapeo;
  2.  
  3. import TDA_Lista.BoundaryViolationException;
  4. import TDA_Lista.DoubleLinkedList;
  5. import TDA_Lista.EmptyListException;
  6. import TDA_Lista.InvalidPositionException;
  7. import TDA_Lista.ListaSimplementeEnlazada;
  8. import TDA_Lista.Position;
  9. import TDA_Lista.PositionList;
  10. import ColaConPrioridad.Entrada;
  11. import ColaConPrioridad.Entry;
  12.  
  13. public class MapeoConLista<K, V> implements Map<K, V> {
  14.  
  15.  
  16. protected DoubleLinkedList<Entry<K,V>> S;
  17.  
  18.  
  19. public int size() {
  20. return S.size();
  21.  
  22. }
  23.  
  24.  
  25. public boolean isEmpty() {
  26. return S.isEmpty();
  27. }
  28.  
  29.  
  30. public V get(K k) {
  31.  
  32. Position<Entry<K,V>> nuevo=null;
  33. try{
  34. if(!S.isEmpty()){
  35. nuevo=S.first();
  36.  
  37. while(nuevo!=null&&nuevo.element().getKey()!=k){
  38. if(nuevo==S.last())
  39. nuevo=null;
  40. else
  41. nuevo=S.next(nuevo);
  42. }
  43. }
  44. }catch(EmptyListException e){
  45. System.out.println(e.getMessage());
  46. }catch(InvalidPositionException e){
  47. System.out.println(e.getMessage());
  48. }catch(BoundaryViolationException e){
  49. System.out.println(e.getMessage());}
  50.  
  51. if (nuevo!=null&&nuevo.element().getKey() == k)
  52. return nuevo.element().getValue();
  53. else
  54. return null;
  55. }
  56.  
  57.  
  58. public V put(K k, V v) {
  59. Entry<K,V> nuevo=new Entrada<K,V>(k,v);
  60. if(S.isEmpty())
  61. S.addFirst(nuevo);
  62. else{
  63. Position<Entry<K,V>> cursor=S.first();
  64. boolean encontre=false;
  65. while(cursor!=null&&!encontre){
  66. if(cursor.element().getKey()==k){
  67. S.set(cursor, nuevo);
  68. encontre=true;
  69. cursor=null;
  70. }
  71. }
  72. if(!encontre)
  73. S.addLast(nuevo);
  74.  
  75. }
  76.  
  77. return null;
  78. }
  79.  
  80.  
  81. public V remove(K k) {
  82. V valor=null;
  83. if(!S.isEmpty()){
  84. Position<Entry<K,V>> cursor=S.first();
  85. boolean encontre=false;
  86.  
  87. while(!encontre&&cursor!=null){
  88. encontre=cursor.element().getKey()==k;
  89. if(encontre)
  90. valor=cursor.element().getValue();
  91. S.remove(cursor);
  92. if(cursor==S.last())
  93. cursor=null;
  94. else
  95. cursor=S.next(cursor);
  96. }
  97. }
  98. return valor;
  99. }
  100.  
  101.  
  102. public Iterable<K> keys() {
  103. PositionList<K>lista= new DoubleLinkedList<K>();
  104. if(!S.isEmpty())
  105. for(Entry<K,V> h: S){
  106. lista.addLast(h.getKey());
  107. }
  108. return lista;
  109. }
  110.  
  111.  
  112. public Iterable<V> values() {
  113. PositionList<V>lista= new DoubleLinkedList<V>();
  114. if(!S.isEmpty())
  115. for(Entry<K,V> h: S){
  116. lista.addLast(h.getValue());
  117. }
  118. return lista;
  119. }
  120.  
  121.  
  122.  
  123. public Iterable<Entry<K, V>> entries() {
  124. PositionList<Entry<K,V>> lista=new DoubleLinkedList<Entry<K,V>>();
  125. if(!S.isEmpty())
  126. for(Entry<K,V> h: S){
  127. lista.addLast(h);
  128. }
  129. return lista;
  130. }
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement