Advertisement
Febrin

Untitled

Mar 19th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. using System;
  2.  
  3. namespace zad2
  4.  
  5. {
  6.     public class Pair<K, V> where K:IComparable
  7.     {
  8.         public K first;
  9.         public V second;
  10.  
  11.         public Pair (K k, V v)
  12.         {
  13.             first = k;
  14.             second = v;
  15.         }
  16.  
  17.     }
  18.    
  19.     public class Vertex<K,V> where K:IComparable
  20.     {
  21.         public Vertex<K,V> prev;
  22.         public Vertex<K,V> next;
  23.         public Pair<K, V> rec;
  24.  
  25.         public Vertex(Pair<K,V> r)
  26.         {
  27.             prev = null;
  28.             next = null;
  29.             rec = r;
  30.         }
  31.     }
  32.  
  33.     public class Dict<K,V> where K:IComparable
  34.     {
  35.         protected Vertex<K,V> first;
  36.         protected Vertex<K,V> last;
  37.         protected int size;
  38.  
  39.         public Dict()
  40.         {
  41.             first = null;
  42.             last = null;
  43.             size = 0;
  44.         }
  45.  
  46.         public int Size() => size;
  47.         public bool IsEmpty()
  48.         {
  49.             if (size == 0)
  50.                 return true;
  51.             return false;
  52.         }
  53.  
  54.         public void Add(K k, V v)
  55.         {
  56.             Vertex<K,V> ver = new Vertex<K,V>(new Pair<K, V>(k,v));
  57.             if (this.IsEmpty())
  58.             {
  59.                 first = ver;
  60.                 last = ver;
  61.             }
  62.             else
  63.             {
  64.                 first.prev = ver;
  65.                 ver.next = first;
  66.                 first = ver;
  67.             }
  68.             size++;
  69.         }
  70.  
  71.         public V Find(K k)
  72.         {
  73.             return this.Findver(k).rec.second;
  74.         }
  75.  
  76.         private Vertex<K,V> Findver(K k)
  77.         {
  78.             Vertex<K, V> ver = first;
  79.             while (k.CompareTo(ver.rec.first)!=0)
  80.             {
  81.                 ver = ver.next;
  82.             }
  83.             return ver;
  84.         }
  85.  
  86.         public void Remove(K k)
  87.         {
  88.             Vertex<K, V> ver = Findver(k);
  89.             ver.prev.next = ver.next;
  90.             ver.next.prev = ver.prev;
  91.             size--;
  92.         }
  93.          
  94.  
  95.     }
  96.  
  97.     class Program
  98.     {
  99.         static void Main()
  100.         {
  101.             Dict<string, int> dict = new Dict<string, int>();
  102.             dict.Add("2+2", 4);
  103.             dict.Add("4-1", 3);
  104.             dict.Add("math", 42);
  105.             Console.WriteLine("Rozmiar slownika: " + dict.Size());
  106.             Console.WriteLine("Szukam:D 2+2: " + dict.Find("2+2"));
  107.             Console.WriteLine("Szukam 4-1 : " + dict.Find("4-1"));
  108.             dict.Remove("4-1");
  109.             Console.WriteLine("Size usunieciu :  " + dict.Size());
  110.         }
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement