Advertisement
Dawid_PAr

listaJednoKier

Aug 18th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include "ListaJedno.cpp"
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. ListaJedno* myList = new ListaJedno;
  9. myList->show();
  10.  
  11. myList->add(10,10);
  12. myList->show();
  13.  
  14. myList->add(20,0);
  15. myList->add(30,1);
  16. myList->add(40,1);
  17. myList->add(50,4);
  18. myList->add(60,100);
  19. myList->show();
  20. myList->rmv(0);
  21. myList->rmv(8);
  22. myList->rmv(1);
  23. myList->rmv(1);
  24. myList->rmv(1);
  25. myList->rmv(1);
  26. myList->rmv(1);
  27. myList->show();
  28. myList->clean();
  29.  
  30.  
  31. return 0;
  32. }
  33.  
  34.  
  35.  
  36. //////////////////////////////////////
  37. PLIK Z LISTA JEDNOKIERUNKOWA
  38. //////////////////////////////////////
  39. //////////////////////////////////////
  40. #include <iostream>
  41. #include <string>
  42. using namespace std;
  43.  
  44. class ElementL
  45. {
  46. public:
  47. int dane;
  48. ElementL* next = NULL;
  49. ElementL(int dane)
  50. {
  51. this->dane = dane;
  52. this->next = NULL;
  53. }
  54. };
  55.  
  56. class ListaJedno
  57. {
  58. public:
  59. ElementL* pierwszy = NULL;
  60. ElementL* ostatni = NULL;
  61.  
  62. ElementL* getPosition(unsigned long long int pos)
  63. {
  64. if(pos < 0 || this->sizee() < pos) return NULL;
  65. ElementL* temp = this->pierwszy;
  66. unsigned long long int i = 1;
  67. while(i++ < pos)
  68. {
  69. temp = temp->next;
  70. }
  71. return temp;
  72. }
  73.  
  74. void add(int d, unsigned long long int pos)
  75. {
  76. ElementL* n = new ElementL(d);
  77. if(this->sizee() == 0)
  78. {
  79. this->pierwszy = n;
  80. this->ostatni = n;
  81. }
  82. else if(pos == 1 || pos == 0)
  83. {
  84. n->next = this->pierwszy;
  85. this->pierwszy = n;
  86. }
  87. else if(pos > this->sizee())
  88. {
  89. this->ostatni->next = n;
  90. this->ostatni = n;
  91. }
  92. else
  93. {
  94. ElementL* temp = getPosition(pos - 1);
  95. n->next = temp->next;
  96. temp->next = n;
  97. }
  98. }
  99.  
  100. void show()
  101. {
  102. if(this->pierwszy == NULL)
  103. {
  104. cout << "BRAK ELEMENTOW" << endl;
  105. return;
  106. }
  107. if(this->sizee() == 1)
  108. {
  109. cout << this->pierwszy->dane << endl;
  110. return;
  111. }
  112. ElementL* temp = this->pierwszy;
  113. while(temp != NULL)
  114. {
  115. cout << temp->dane << endl;
  116. temp = temp->next;
  117. }
  118. }
  119.  
  120. bool rmv(unsigned long long int pos)
  121. {
  122. if(this->sizee() == 0) return false;
  123. if(pos == 0 || pos > this->sizee())
  124. {
  125. cout << "PODANO ZLA POZYCJE" << endl;
  126. return false;
  127. }
  128. if(pos == 1)
  129. {
  130. ElementL* temp = this->pierwszy;
  131. this->pierwszy = this->pierwszy->next;
  132. delete temp;
  133. }
  134. else if(pos == sizee())
  135. {
  136. ElementL* temp = this->getPosition(pos - 1);
  137. delete temp->next;
  138. temp->next = NULL;
  139. this->ostatni = temp;
  140. }
  141. else
  142. {
  143. ElementL* temp = this->getPosition(pos - 1);
  144. ElementL* temp1 = this->getPosition(pos);
  145. temp->next = temp1->next;
  146. delete temp1;
  147. }
  148. return true;
  149. }
  150.  
  151. unsigned long long int sizee()
  152. {
  153. ElementL* temp = this->pierwszy;
  154. int i = 0;
  155. while(temp != NULL)
  156. {
  157. temp = temp->next;
  158. i+=1;
  159. }
  160. return i;
  161. }
  162.  
  163. void clean()
  164. {
  165. ElementL* temp = this->pierwszy;
  166. while(temp != NULL)
  167. {
  168. delete temp;
  169. temp = temp->next;
  170. }
  171. }
  172. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement