Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Zadanie4
  5. {
  6.     public class Pair<T, U>
  7.     {
  8.         public Pair() {
  9.         }
  10.  
  11.         public Pair(T first, U second) {
  12.             this.First = first;
  13.             this.Second = second;
  14.         }
  15.  
  16.         public T First { get; set; }
  17.         public U Second { get; set; }
  18.     }
  19.  
  20.     public class Słownik<K,V>
  21.     {
  22.         private static int SIZE = 1000000;
  23.         public int index;
  24.         Pair<K,V>[] słownik = new Pair<K,V>[SIZE];
  25.  
  26.         public void Add (K key, V value)
  27.         {
  28.             Pair<K,V> para = new Pair<K,V>(key, value);
  29.             słownik[index] = para;
  30.             index += 1;
  31.         }
  32.  
  33.         public void Search(K key)
  34.         {
  35.  
  36.             for (int i = 0; i < index; i++)
  37.             {
  38.                 if(object.Equals(słownik[i].First, key))
  39.                 {
  40.                     Console.WriteLine("{0}",słownik[i].Second);
  41.                     break;
  42.                 }
  43.             }
  44.         }
  45.  
  46.         public void Delete(K key)
  47.         {
  48.             int i = 0;
  49.             int j = 0;
  50.             int len = index - 1;
  51.             Pair<K,V>[] słownik1 = new Pair<K,V>[SIZE];
  52.             do
  53.             {
  54.                 if(object.Equals(słownik[j].First,key))
  55.                 {
  56.                     j += 1;
  57.                     continue;
  58.                 }
  59.                 słownik1[i] = słownik[j];
  60.                 j += 1;
  61.                 i += 1;
  62.  
  63.             }
  64.             while(i != len);
  65.             index -= 1;
  66.             słownik = słownik1;
  67.         }
  68.  
  69.         public Słownik()
  70.         {
  71.             index = 0;
  72.         }
  73.  
  74.         public void wypisz()
  75.         {
  76.             for(int i = 0; i < index; i++)
  77.             {
  78.                 Console.WriteLine("klucz: {0}, wartość: {1}", słownik[i].First, słownik[i].Second);
  79.             }
  80.         }
  81.     }
  82.  
  83.     class MainClass
  84.     {
  85.         public static void Main(string[] args)
  86.         {
  87.             Słownik<string,int> dict = new Słownik<string,int>();
  88.             dict.Add("Klucz1", 1);
  89.             dict.Add("Klucz2", 2);
  90.             dict.Add("Klucz3", 3);
  91.             dict.Add("Klucz4", 4);
  92.             dict.wypisz();
  93.             dict.Search("Klucz2");
  94.             dict.Delete("Klucz2");
  95.             dict.wypisz();
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement