Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Lista {
  6. private:
  7. typedef struct element {
  8. string slowo;
  9. int wartosc;
  10. element* nastepny;
  11. element* poprzedni;
  12.  
  13. }element;
  14. element* pozycja_pierwszego;
  15. element* pozycja_ost;
  16.  
  17. int size;
  18. int ile;
  19. public:
  20. Lista();
  21. element* aktualny;
  22. ~Lista();
  23. void insert(string, int, int);
  24. void erase(element*);
  25. void push_front(string, int);
  26. void push_back(string, int);
  27. int get_size();
  28. void przesuniecie(int);
  29. void aktualnie(int);
  30. element* akt();
  31.  
  32.  
  33. };
  34.  
  35. int main()
  36. {
  37. string slowo;
  38. int wartosc;
  39. Lista list;
  40. int rozmiar_listy;
  41. int indeks_pierwszego;
  42. cin >> rozmiar_listy >> indeks_pierwszego;
  43. for (int i = 0; i < rozmiar_listy; i++)
  44. {
  45. cin >> slowo >> wartosc;
  46. list.push_back(slowo, wartosc);
  47. }
  48. list.aktualnie(indeks_pierwszego);
  49.  
  50. // list.display_test();
  51. // list.erase(&indeks_pierwszego);
  52. // list.display_test();
  53. int rozmiar_drugiej;
  54. cin >> rozmiar_drugiej;
  55. while (list.get_size() > 0)
  56. {
  57. if (list.akt()->wartosc == 0)
  58. {
  59. int war;
  60. cin >> slowo >> wartosc >> war;
  61. list.insert(slowo, wartosc, war);
  62. list.przesuniecie(war);
  63. }
  64. else
  65. {
  66. //wyswietlamy slowo
  67. cout << list.akt()->slowo << " ";
  68. //jezeli jest dodatni idziemy o krok do przodu,
  69. if (list.akt()->wartosc > 0)
  70. {
  71. int wart = list.aktualny->wartosc;
  72. list.aktualny = list.aktualny->nastepny;
  73. list.erase(list.aktualny->poprzedni);
  74. list.przesuniecie(wart - 1);
  75. }
  76. //jezeli ujemny to do tylu
  77. else
  78. {
  79. int wart = list.aktualny->wartosc;
  80. list.aktualny = list.aktualny->poprzedni;
  81. list.erase(list.aktualny->nastepny);
  82. list.przesuniecie(wart + 1);
  83. }
  84.  
  85.  
  86.  
  87.  
  88. //list.erase(list.akt());
  89.  
  90. }
  91. }
  92. return 0;
  93. }
  94. Lista::Lista()
  95. {
  96. pozycja_pierwszego = pozycja_ost = nullptr;
  97. size = 0;
  98. }
  99.  
  100. Lista::~Lista()
  101. {
  102.  
  103. }
  104. void Lista::insert(string slowo, int wartosc, int war)
  105. {
  106. element* el = aktualny;
  107. element* nowy = new element;
  108. nowy->slowo = slowo;
  109. nowy->wartosc = wartosc;
  110. el->poprzedni->nastepny = nowy;
  111. nowy->poprzedni = el->poprzedni;
  112. el->wartosc = war;
  113. nowy->nastepny = el;
  114. el->poprzedni = nowy;
  115. size++;
  116. if (this->aktualny == this->pozycja_pierwszego)
  117. this->pozycja_pierwszego = aktualny->poprzedni;
  118.  
  119.  
  120. }
  121.  
  122. void Lista::erase(element* el) //to tak naprawde nie jest indeks pierwszego xDD
  123. {
  124. int war = this->aktualny->wartosc;
  125. el->poprzedni->nastepny = el->nastepny;
  126. el->nastepny->poprzedni = el->poprzedni;
  127. if (el == pozycja_ost)
  128. pozycja_ost = el->poprzedni;
  129. if (el == pozycja_pierwszego)
  130. pozycja_pierwszego = el->nastepny;
  131. if (this->size == 1)
  132. {
  133. pozycja_pierwszego = pozycja_ost = NULL;
  134. size = 0;
  135. delete el;
  136. return;
  137. }
  138. size--;
  139. delete el;
  140.  
  141.  
  142. }
  143. void Lista::push_front(string slowo, int wartosc)
  144. {
  145. element* pomocniczy = new element;
  146. pomocniczy->slowo = slowo;
  147. pomocniczy->wartosc = wartosc;
  148. pomocniczy->poprzedni = pomocniczy;
  149. pomocniczy->nastepny = pomocniczy;
  150. pozycja_pierwszego = pomocniczy;
  151. pozycja_ost = pomocniczy;
  152. aktualny = pozycja_pierwszego;
  153. }
  154.  
  155. void Lista::push_back(string slowo, int wartosc)
  156. {
  157. if (pozycja_pierwszego == nullptr)
  158. push_front(slowo, wartosc);
  159. else
  160. {
  161. element* nowy = new element;
  162. nowy->slowo = slowo;
  163. nowy->wartosc = wartosc;
  164. nowy->nastepny = pozycja_pierwszego;
  165. nowy->poprzedni = pozycja_ost;
  166. pozycja_ost->nastepny = nowy;
  167. pozycja_pierwszego->poprzedni = nowy;
  168. pozycja_ost = nowy;
  169. }
  170. size++;
  171. }
  172.  
  173. int Lista::get_size()
  174. {
  175. return size;
  176. }
  177. void Lista::przesuniecie(int ile)
  178. {
  179. if (size > 0)
  180. {
  181. if (ile > 0 && size > 0)
  182. ile = ile % size;
  183.  
  184. if (ile > 0)
  185. {
  186. for (int i = 0; i < ile; i++)
  187. aktualny = aktualny->nastepny;
  188.  
  189. }
  190. if (ile < 0)
  191. {
  192.  
  193. for (int i = 0; i < std::abs(ile); i++)
  194. aktualny = aktualny->poprzedni;
  195.  
  196. }
  197.  
  198. }
  199. }
  200. /*void Lista::przesuniecie(int ile)
  201. {
  202. ile = ile%size;
  203. if(size == 2)
  204. {
  205. aktualny = aktualny->nastepny;
  206. return;
  207. }
  208. // if(size < 2)
  209. // return;
  210. float polowa = (float)(size)/2;
  211. if(ile < 0)
  212. {
  213. if(ile*-1>= polowa)
  214. {
  215. for(int i = (size + ile);i>0;i--)
  216. {
  217.  
  218. aktualny = aktualny->nastepny;
  219. }
  220. }
  221. else
  222. for(int i = 0; i < ile*-1; i++)
  223. aktualny = aktualny->poprzedni;
  224. }
  225. else if(ile > 0)
  226. {
  227. if(ile >= polowa)
  228. {
  229. for(int i = (size +ile)%size;i>0;i--)
  230. {
  231.  
  232. aktualny = aktualny->poprzedni;
  233. }
  234. }
  235. else
  236. for(int i = 0; i < ile; i++)
  237. aktualny = aktualny->nastepny;
  238.  
  239. }
  240.  
  241. }
  242. */
  243. void Lista::aktualnie(int indeks)
  244. {
  245.  
  246. element* el = aktualny;
  247. for (int i = 0; i < indeks; i++)
  248. el = el->nastepny;
  249. aktualny = el;
  250. }
  251.  
  252. Lista::element* Lista::akt()
  253. {
  254. return aktualny;
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement