Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace ConsoleApp2
  8. {
  9. class Kolejka<T>
  10. {
  11. private T[] tablica;
  12. private int head, tail;
  13. private bool start = true;
  14. public Kolejka(int rozmiar)
  15. {
  16. tablica = new T[rozmiar];
  17. }
  18.  
  19. public Kolejka()
  20. {
  21. tablica = new T[10];
  22. }
  23.  
  24. public void DodajListe(T[] tab)
  25. {
  26. int count = 0;
  27. foreach(var el in tab)
  28. {
  29. if (Dodaj(el))
  30. count++;
  31. }
  32. Console.WriteLine("Dodano "+count+" elementow");
  33. }
  34.  
  35. public bool Dodaj(T element)
  36. {
  37. if (CzyPelna())
  38. return false;
  39.  
  40. tablica[tail] = element;
  41. tail = (tail + 1) % tablica.Length;
  42. if (start)
  43. start = false;
  44.  
  45. return true;
  46. }
  47.  
  48. public void Usun()
  49. {
  50. if (CzyPusta())
  51. return;
  52.  
  53. if ((head+1) % tablica.Length == ((tail) % tablica.Length))
  54. {
  55. head = tail = 0;
  56. start = true;
  57. }
  58. else
  59. {
  60. head = (head + 1) % tablica.Length;
  61. }
  62. }
  63.  
  64. public bool CzyPusta()
  65. {
  66. if (CzyPelna())
  67. return false;
  68.  
  69. if ((tail) % tablica.Length == (head) % tablica.Length && !start || start)
  70. {
  71. return true;
  72. }
  73. return false;
  74. }
  75.  
  76. public void Drukuj()
  77. {
  78. int i = 0;
  79. Console.WriteLine("Head:"+head +" Tail:"+tail);
  80. foreach(T f in tablica)
  81. {
  82. Console.WriteLine("["+i+"] "+f);
  83. i++;
  84. }
  85. Console.WriteLine();
  86. }
  87.  
  88. public void Status()
  89. {
  90. Console.WriteLine("Head == "+head);
  91. Console.WriteLine("Tail == " + tail);
  92. Console.WriteLine("Pusta? == " + CzyPusta());
  93. Console.WriteLine("Pelna? == "+ CzyPelna());
  94. }
  95.  
  96. /*
  97. head = 0 tail = 0
  98. head = 0 tail = 1
  99. head = 0 tail = 2
  100. head = 0 tail = 3//tablica full*/
  101.  
  102.  
  103.  
  104. public bool CzyPelna()
  105. {
  106. if ((tail) % tablica.Length == (head) % tablica.Length && !start)
  107. {
  108. return true;
  109. }
  110.  
  111.  
  112. return false;
  113. }
  114.  
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement