Advertisement
Guest User

Lancolt

a guest
Mar 29th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.35 KB | None | 0 0
  1.  
  2. Edit with the Docs app
  3. Make tweaks, leave comments, and share with others to edit at the same time.
  4. NO THANKSUSE THE APP
  5.  
  6. 7gyak
  7.  
  8. 7. Gyak
  9. Generikus típusok
  10. Default kulcssszt is érdemes néha használni:
  11. class Minta
  12. {
  13. T a;
  14. public Minta( )
  15. {
  16. a = default(T);
  17. }
  18. }
  19. Megszorítások
  20. Lehet érték vagy referencia típúsú, d emégjobb ha konkrétabb pl interface (IComparable)
  21. Vermes generikus kód:
  22. using System;
  23. using System.Collections.Generic;
  24. using System.Linq;
  25. using System.Text;
  26. using System.Threading.Tasks;
  27. namespace _7gyak
  28. {
  29. class Verem<typeParam>
  30. {
  31. typeParam[] tomb; //typeParam egy kulcsszó, típusparaméter
  32. int pointer; //kövi elem helyét mutatja
  33. public Verem(int meret)
  34. {
  35. tomb = new typeParam[meret];
  36. pointer = 0;
  37. }
  38. public void Push(typeParam elem)
  39. {
  40. this.tomb[pointer++] = elem;
  41. }
  42. //de meg kéne oldani, hogy ne csak inteket rakhassak bele, mert útközbe meggondolom magam--> generikus típusok kellenek, de favágó módszer: object típusú tömbök (object[] tomb és minden átírva), de itt kasztolni kéne
  43. public object Pop()
  44. {
  45. return this.tomb[--pointer];
  46. }
  47. }
  48. class Program
  49. {
  50. static void Main(string[] args)
  51. {
  52. Verem<string> V1 = new Verem<string>(5); //it tmondom meg hogy ez a típusparaméter, azaz V1-be stringeket tudok beletenmni
  53. V1.Push("dfghj");
  54. Console.WriteLine(V1.Pop());
  55. Verem<int> V2 = new Verem<int>(5);
  56. V2.Push(3);
  57. Console.WriteLine(V2.Pop());
  58. }
  59. }
  60. }
  61. --------------------------------------------------------------------------------
  62. using System;
  63. using System.Collections.Generic;
  64. using System.Linq;
  65. using System.Text;
  66. using System.Threading.Tasks;
  67. using System.Diagnostics; //stopwatch timer miatt használjuk
  68. namespace _7gyak
  69. {
  70. class Stack<T>
  71. {
  72. }
  73. class Verem<typeParam>
  74. {
  75. typeParam[] tomb; //typeParam egy kulcsszó, típusparaméter
  76. int pointer; //kövi elem helyét mutatja
  77. public Verem(int meret)
  78. {
  79. tomb = new typeParam[meret];
  80. pointer = 0;
  81. }
  82. public void Push(typeParam elem)
  83. {
  84. this.tomb[pointer++] = elem;
  85. }
  86. //de meg kéne oldani, hogy ne csak inteket rakhassak bele, mert útközbe meggondolom magam--> generikus típusok kellenek, de favágó módszer: object típusú tömbök (object[] tomb és minden átírva), de itt kasztolni kéne
  87. public object Pop()
  88. {
  89. return this.tomb[--pointer];
  90. }
  91. }
  92. class Program
  93. {
  94. //static void Ize(int p1, int p2);
  95. static void Main(string[] args)
  96. {
  97. //Verem<string> V1 = new Verem<string>(5); //it tmondom meg hogy ez a típusparaméter, azaz V1-be stringeket tudok beletenmni
  98. //V1.Push("dfghj");
  99. //Console.WriteLine(V1.Pop());
  100. //Verem<int> V2 = new Verem<int>(5);
  101. //V2.Push(3);
  102. //Console.WriteLine(V2.Pop());
  103. //-----------------------------------------
  104. //Stack<string> V1 = new Stack<string>();
  105. //Dictionary<string,int> myDict = new Dictionary<string,int>();
  106. //myDict.Add("eletkor", 18); //kulcs: életkor, értéke: 18
  107. //myDict.Add("Iranyitoszam", 1034);
  108. //myDict.Add("ztestmagassag", 180);
  109. //Console.WriteLine(myDict["eletkor"]); //kiírja h 18
  110. //foreach (var item in myDict.Values) //hanincs itt a .Values, akkor a kuclsokat is kiírja, így csak az értékeket
  111. //{
  112. // Console.WriteLine(item);
  113. //}
  114. Stopwatch SW = new Stopwatch();
  115. SW.Start();
  116. List<string> L1 = new List<string>();
  117. int S = int.Parse(Console.ReadLine());
  118. //L1.Add("JHJK");
  119. for (int i = 0; i < S; i++)
  120. {
  121. L1.Add("WOW");
  122. }
  123. //LinkedList<string> L2 = new LinkedList<string>();
  124. //for (int i = 0; i < 10000; i++)
  125. //{
  126. // L2.AddFirst("WOW");
  127. //}
  128. SW.Stop();
  129. Console.WriteLine(SW.ElapsedMilliseconds);
  130. Console.ReadLine();
  131.  
  132. }
  133. }
  134. }
  135. -------------------------------------------------------------------
  136. Saját lista készítése
  137. using System;
  138. using System.Collections.Generic;
  139. using System.Linq;
  140. using System.Text;
  141. using System.Threading.Tasks;
  142. using System.Diagnostics;
  143. namespace _7gyak
  144. {
  145. class LancoltLista<T>
  146. {
  147. //láncolt lista feje egy referencia a láncolt lista elemére
  148. //mutató értékpárokat tárolok--> beágyazott osztályt használok
  149. class ListaElem
  150. {
  151. public T ertek;
  152. public ListaElem kovetkezo; //mutató
  153. public ListaElem(T ertek)
  154. {
  155. this.ertek = ertek;
  156. //this.kovetkezo = kovetkezo;
  157. }
  158. }
  159. ListaElem fej=null;
  160. private ListaElem AdottErtekElottiElem(T ertek)
  161. {
  162. ListaElem aktualisElem = fej;
  163. ListaElem megelozoElem = null;
  164. while (aktualisElem != null && aktualisElem.ertek.Equals(ertek))
  165. {
  166. megelozoElem = aktualisElem;
  167. aktualisElem = aktualisElem.kovetkezo;
  168. }
  169. if (aktualisElem == null)
  170. {
  171. return null;
  172. }
  173. else
  174. {
  175. return megelozoElem;
  176. }
  177. }
  178. public void AdottErtekEleBeszurt(T adottErtek, T ujErtek)
  179. {
  180. ListaElem uj = new ListaElem(ujErtek);
  181. ListaElem elozo = AdottErtekElottiElem(ujErtek);
  182. uj.kovetkezo = elozo.kovetkezo;
  183. if (elozo != null)
  184. {
  185. uj.kovetkezo = elozo.kovetkezo;
  186. elozo.kovetkezo = uj;
  187. }
  188. }
  189. public void EloreBeszur(T ertek)
  190. {
  191. ListaElem ujElem = new ListaElem(ertek);
  192. ujElem.kovetkezo = fej; //itt egy másolat történik
  193. fej = ujElem;
  194. }
  195. public void Bejar()
  196. {
  197. //végigmegy a listán és kiírja a konzolra az értékeket
  198. ListaElem aktualisElem = fej;
  199. while (aktualisElem != null)
  200. {
  201. Console.WriteLine(aktualisElem.ertek);
  202. aktualisElem = aktualisElem.kovetkezo;
  203. }
  204. }
  205.  
  206. }
  207.  
  208. class Program
  209. {
  210.  
  211. static void Main(string[] args)
  212. {
  213. LancoltLista<string> myLL = new LancoltLista<string>();
  214. myLL.EloreBeszur("elso");
  215. myLL.EloreBeszur("masodik");
  216. myLL.EloreBeszur("harmadik");
  217.  
  218. myLL.AdottErtekEleBeszur("masodik", "WOW");
  219. myLL.Bejar();
  220. myLL.Bejar();
  221. Console.ReadLine();
  222.  
  223. }
  224. }
  225. }
  226. Befejezni, mert nem működik
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement