fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. // Struktura Aktor przechowujaca dane aktora - lista aktorów.
  6. struct Aktor {
  7. string nazwisko; // Imię i nazwisko razem - dla ułatwienia.
  8. int lf; // Liczba fanów, którzy przestaną oglądać serial po odejściu aktora.
  9. Aktor *next; // Wskaźnik na następny element listy.
  10. Aktor(string nazwisko, int lf);
  11. };
  12.  
  13. struct Lista {
  14. Aktor *first; // wskaźnik na poczatek listy
  15. Lista();
  16. void insert_aktor(string nazwisko, int lf);
  17. void find_aktor(int p);
  18. Aktor * AktorSearch(int lfs);
  19. };
  20.  
  21. Aktor::Aktor(string nazwisko, int lf) {
  22. next = NULL;
  23. this->nazwisko = nazwisko;
  24. this->lf = lf;
  25. }
  26.  
  27. Lista::Lista() {
  28. first = NULL;
  29. }
  30.  
  31.  
  32. void Lista::insert_aktor(string nazwisko, int lf) {
  33. Aktor *nowy = new Aktor(nazwisko, lf);
  34. Aktor *pomoc = first, *pomoc2 = NULL; // Wskazniki pomocnicze do poruszania się po liscie.
  35.  
  36. if (first == NULL) {
  37. nowy->next = NULL;
  38. first = nowy;
  39. cout << "TAK" << endl;
  40. }
  41. else {
  42. while ((pomoc != NULL) && ((pomoc->lf) < (nowy->lf))) { // Dopoki nie wyszlismy za liste i aktor na liscie jest przed nowym aktorem, poruszamy sie dalej.
  43. pomoc2 = pomoc; // pomoc2 zawsze wskazuje poprzednika pomoc.
  44. pomoc = pomoc->next;
  45. }
  46. if (pomoc == NULL) {
  47. nowy->next = NULL;
  48. pomoc2->next = nowy;
  49. cout << "TAK" << endl;
  50. }
  51. else if ((pomoc->lf) == (nowy->lf)) {
  52. if (((pomoc->nazwisko) == (nowy->nazwisko))){ // Aktor z podanym nazwiskiem juz istnieje.
  53. cout << "NIE" << endl;
  54. delete nowy; // Usuniecie nowego obiektu aktora
  55. }
  56. else if (((pomoc->nazwisko) < (nowy->nazwisko))) {
  57. //jak nowe nazwisko wieksze:
  58. if (pomoc->next == NULL) {
  59. nowy->next = NULL;
  60. pomoc->next = nowy;
  61. cout << "TAK" << endl;
  62. }
  63. else {
  64. nowy->next = pomoc->next;
  65. pomoc->next = nowy;
  66. cout << "TAK" << endl;
  67. }
  68. }
  69. else {
  70. //jak nowe nazwisko mniejsze:
  71. if (pomoc == first) {
  72. nowy->next = first;
  73. first = nowy;
  74. cout << "TAK" << endl;
  75. }
  76. else{
  77. nowy->next = pomoc;
  78. pomoc2->next = nowy;
  79. cout << "TAK" << endl;
  80. }
  81.  
  82. }
  83. /*else {
  84. if (pomoc->nazwisko > nowy->nazwisko) {
  85. nowy->next = pomoc->next;
  86. pomoc->next = nowy;
  87. cout << "TAK" << endl;
  88. }
  89. else {
  90. if (pomoc == first) {
  91. nowy->next = pomoc;
  92. first = nowy;
  93. cout << "TAK" << endl;
  94. }
  95. else {
  96. nowy->next = pomoc;
  97. pomoc2->next = nowy;
  98. cout << "TAK" << endl;
  99. }
  100. }
  101. }*/
  102. }
  103. else if ((pomoc->lf) < (nowy->lf)) {
  104. // nowy->lf wiekszy:
  105. if (pomoc->next == NULL) {
  106. nowy->next = NULL;
  107. pomoc->next = nowy;
  108. cout << "TAK" << endl;
  109. }
  110. else {
  111. nowy->next = pomoc->next;
  112. pomoc->next = nowy;
  113. cout << "TAK" << endl;
  114. }
  115. }
  116. else {
  117. //jak nowe->lf mniejsze:
  118. if (pomoc == first) {
  119. nowy->next = first;
  120. first = nowy;
  121. cout << "TAK" << endl;
  122. }
  123. else{
  124. nowy->next = pomoc;
  125. pomoc2->next = nowy;
  126. cout << "TAK" << endl;
  127. }
  128. }
  129. }
  130. }
  131.  
  132.  
  133. void Lista::find_aktor(int p){
  134. Aktor *pomoc = first, *pomoc2 = NULL, *pom, *pom2, *out;
  135. bool tmp = true;
  136. if (pomoc == NULL){
  137. cout << "NIE" << endl;
  138. }
  139. else{
  140. while ((pomoc != NULL )) {
  141. if (p <= pomoc->lf){
  142. pom = pomoc;
  143. tmp = true;
  144. break;
  145. }
  146. else{
  147. tmp = false;
  148. }
  149. if (pomoc->next == NULL || pomoc->next->lf > p) {
  150. pom2 = pomoc2;
  151. }
  152.  
  153. pomoc2 = pomoc;
  154. pomoc = pomoc->next;
  155. }
  156. if (tmp == true){
  157.  
  158. if (pom->lf == p) {
  159.  
  160. if (pom == first) {
  161. cout << pom->nazwisko << endl;
  162. first = first->next;
  163. }
  164. else if (pomoc->next == NULL) {
  165. //koniec listy:
  166. cout << pom->nazwisko << endl;
  167. pomoc2->next = NULL;
  168. }
  169. else {
  170. //w srodku:
  171. cout << pom->nazwisko << endl;
  172. pomoc2->next = pom->next;
  173. }
  174. }
  175. else if (pomoc->lf < p) {
  176. cout << pomoc2->nazwisko << endl;
  177. if (pomoc == first) {
  178. pomoc->next = NULL;
  179. first = NULL;
  180. }
  181. else {
  182. pomoc2->next = NULL;
  183. }
  184. }
  185. else if (p < pom->lf){
  186. if (pom == first) {
  187. cout << "NIE" << endl;
  188. }
  189. else {
  190. cout << pomoc2->nazwisko << endl;
  191. if (pomoc2 == first) {
  192. first = pomoc;
  193. }
  194. else {
  195. pom2->next = pomoc;
  196. }
  197. }
  198. }
  199. }
  200. else if (pomoc2->lf < p) {
  201. cout << pomoc2->nazwisko << endl;
  202. if (pomoc2 == first) {
  203. pomoc2->next = NULL;
  204. first = NULL;
  205. }
  206. else {
  207. pom2->next = NULL;
  208. }
  209.  
  210. }
  211. else{
  212. cout << "NIE" << endl;
  213. }
  214. }
  215.  
  216. }
  217.  
  218. Aktor * Lista::AktorSearch(int lfs) {
  219. Aktor *x = first;
  220. while (x != NULL && x->lf != lfs) {
  221. x = x->next;
  222. }
  223. return x;
  224. }
  225.  
  226. int main() {
  227.  
  228. int n; // Liczba operacji do wykonania.
  229. int op; // Rodzaj operacji do wykonania.
  230. int p; // Maksymalna liczba fanów, na której stratę można sobie pozwolić.
  231. int lf;
  232. string nazwisko;
  233.  
  234. cin >> n;
  235.  
  236. Lista *lista = new Lista;
  237.  
  238. for (int i = 0; i < n; i++) {
  239. cin >> op; // Wczytanie rodzaju operacji.
  240. if (op == 1){
  241.  
  242. cin.ignore();
  243. getline(cin, nazwisko);
  244. cin >> lf;
  245.  
  246. lista->insert_aktor(nazwisko, lf);
  247. }
  248. if (op == 2){ // Wyszukanie aktora do roli.
  249. cin.ignore();
  250. cin >> p; // Podaj liczbę fanów, na której stratę możesz sobie pozwolić.
  251. lista->find_aktor(p);
  252. }
  253. }
  254.  
  255. /*lista->insert_aktor("Jurek", 200);
  256. lista->insert_aktor("antek", 100);
  257. lista->insert_aktor("tomek", 400);
  258. lista->insert_aktor("zenek", 200);
  259. lista->insert_aktor("juntek", 300);
  260. lista->insert_aktor("marek", 600);
  261.  
  262. lista->find_aktor(100);
  263. lista->insert_aktor("jozin", 100);
  264.  
  265. cout << lista->first->nazwisko << endl;
  266. cout << lista->first->next->nazwisko << endl;
  267. cout << lista->first->next->next->nazwisko << endl;
  268. cout << lista->first->next->next->next->nazwisko << endl;
  269. cout << lista->first->next->next->next->next->nazwisko << endl;*/
  270.  
  271. //system("pause");
  272. return 0;
  273. }
Success #stdin #stdout 3.14s 3432KB
stdin
Standard input is empty
stdout
Standard output is empty