Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace p23_LSI
  8. {
  9. class LSI
  10. {
  11. Nod L; // referinta la liste
  12.  
  13. //constructorul care construieste lista
  14. //cu un nod care retine valoarea val
  15.  
  16. public LSI(int val)
  17. {
  18. L = new Nod(val);
  19. }
  20.  
  21. public LSI()
  22. {
  23. L = null;
  24. }
  25.  
  26. //Destructorul
  27. ~LSI()
  28. {
  29. L = null;
  30. }
  31.  
  32. //Sterge al k-lea nod din lista
  33. public void Erase(int k)
  34. {
  35. Nod p, q;
  36. int n = Count;
  37. if (k <= 0 || k > n) return;
  38. if (k == 1)
  39. {
  40. p = L;
  41. L = L.leg;
  42. p = null;
  43. return;
  44. }
  45. //ma pozitionez pe al k-1 lea nod ca sa pot sterge nodul urmator
  46. for (p = L; k > 2; p = p.leg)
  47. k--;
  48. q = p.leg;
  49. p.leg = q.leg;
  50. q = null;
  51. }
  52.  
  53. //indexator
  54. public int this[int i]
  55. {
  56. get
  57. {
  58. int n = Count;
  59. if (i <= 0) i = 1;
  60. if (i > n) i = n;
  61. Nod p;
  62. for (p = L; p != null && i > 1; p = p.leg)
  63. {
  64. i--;
  65. }
  66. return p.info;
  67. }
  68. set
  69. {
  70. int n = Count;
  71. if (i <= 0) i = 1;
  72. if (i > n) i = n;
  73. Nod p;
  74. for (p = L; p != null && i > 1; p = p.leg)
  75. {
  76. i--;
  77. }
  78. p.info = value;
  79. }
  80. }
  81.  
  82. //adaugare a unui nod LA INCEPUTUL LISTEI
  83. public void AddBegin(int val)
  84. {
  85. Nod p = new Nod(val);
  86. p.leg = L;
  87. L = p;
  88. }
  89.  
  90. public void Add(int val, int k)
  91. {
  92. int n = Count;
  93. if (k <= 1) //adauga la inceput in lista
  94. {
  95. AddBegin(val);
  96. return;
  97. }
  98. Nod p, q;
  99. q = L;
  100. for (p = L; p != null && k > 1; p = p.leg)
  101. {
  102. k--;
  103. q = p;
  104. }
  105. //inserare dupa nodul p
  106. p = new Nod(val);
  107. p.leg = q.leg;
  108. q.leg = p;
  109. }
  110.  
  111. public int Count
  112. {
  113. get
  114. {
  115. int cnt = 0;
  116. for (Nod p = L; p!=null; p=p.leg)
  117. {
  118. cnt++;
  119. }
  120. return cnt;
  121. }
  122. }
  123.  
  124. public override string ToString()
  125. {
  126. string s = "";
  127. for (Nod p = L; p!=null; p=p.leg)
  128. {
  129. s += p.info + " ";
  130. }
  131. return s;
  132. }
  133.  
  134. //redefinirea operatorilor
  135. public static LSI operator +(LSI A, LSI B)
  136. {
  137. LSI C = new LSI();
  138. Nod p;
  139. int k=0;
  140. for(p = A.L; p!=null; p = p.leg)
  141. {
  142. k++;
  143. C.Add(p.info, k);
  144. }
  145. for (p = B.L; p != null; p = p.leg)
  146. {
  147. k++;
  148. C.Add(p.info, k);
  149. }
  150. return C;
  151. }
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement