Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. using System;
  2.  
  3. namespace lab4._2
  4. {
  5. public class Osoba
  6. {
  7. string imie;
  8. string nazwisko;
  9.  
  10. public Osoba()
  11. {
  12. imie = "nieznane";
  13. nazwisko = "nieznane";
  14. }
  15. public Osoba(string imie_, string nazwisko_)
  16. {
  17. imie = imie_;
  18. nazwisko = nazwisko_;
  19. }
  20. public void WypiszInfo()
  21. {
  22. Console.WriteLine("Imię i nazwisko: " + imie + " " + nazwisko);
  23. }
  24. }
  25.  
  26. public class Lista
  27. {
  28. private class Element
  29. {
  30. public Osoba wartosc;
  31. public Element nastepnyElement = null;
  32.  
  33. public Element(Osoba o)
  34. {
  35. wartosc = o;
  36. }
  37. }
  38.  
  39. Element pierwszyElement;
  40. int liczbaElementow = 0;
  41.  
  42. public int LiczbaElementow
  43. {
  44. get => liczbaElementow;
  45. }
  46. public void Dodaj(Osoba wartosc)
  47. {
  48. var k = pierwszyElement;
  49. while(k != null)
  50. {
  51. k = k.nastepnyElement;
  52. }
  53. k = new Element(wartosc);
  54. liczbaElementow++;
  55. }
  56. public object Pobierz(int indeks)
  57. {
  58. if (indeks >= 0 && indeks < liczbaElementow)
  59. {
  60. object t = pierwszyElement.wartosc;
  61. pierwszyElement.wartosc = null;
  62. pierwszyElement = pierwszyElement.nastepnyElement;
  63. liczbaElementow--;
  64. return t;
  65. }
  66. else
  67. {
  68. Console.WriteLine("Brak elementów!");
  69. return null;
  70. }
  71. }
  72. /*public object Wstaw(Osoba o, int indeks)
  73. {
  74. if(indeks > 0 && indeks =< liczbaElementow)
  75. {
  76.  
  77. }
  78. else
  79. {
  80. Console.WriteLine("Brak elementów!");
  81. return null;
  82. }
  83. }*/
  84. public void Wypisz()
  85. {
  86. var p = pierwszyElement;
  87. while (p != null)
  88. {
  89. p.wartosc.WypiszInfo();
  90. p = p.nastepnyElement;
  91. }
  92. }
  93. }
  94.  
  95. class Program
  96. {
  97. static void Main(string[] args)
  98. {
  99. Osoba o = new Osoba("Alicja", "Nowak");
  100. Osoba o2 = new Osoba("Karolina", "Kowalska");
  101. Osoba o3 = new Osoba("Michał", "Jabłoński");
  102. Osoba o4 = new Osoba("Karol", "Wiśniewski");
  103.  
  104. Lista l = new Lista();
  105.  
  106. l.Dodaj(o);
  107. l.Dodaj(o2);
  108. l.Dodaj(o3);
  109. l.Dodaj(o4);
  110.  
  111. l.Wypisz();
  112.  
  113. //l.Pobierz(2);
  114. //l.Pobierz(0);
  115. //l.Pobierz(1);
  116.  
  117. l.Wypisz();
  118.  
  119. //l.Wstaw(o3, 0);
  120. //l.Wstaw(o4, 1);
  121. //l.Wstaw(o, 2);
  122.  
  123. l.Wypisz();
  124. }
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement