fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. /*
  6.   ________
  7.   | Data |
  8.   |________|
  9.   | *next |
  10.   |________|
  11. */
  12.  
  13. struct Data
  14. {
  15. int data;
  16. Data *next;
  17.  
  18. Data(int); // Data(int val)
  19. ~Data();
  20. void print();
  21. };
  22. // konstruktor
  23. Data :: Data(int val)
  24. {
  25. data = val;
  26. next = NULL;
  27. // Ponizej jest rysunek jak wyglada lista
  28. // - element i wskaznik na kolejny element
  29. // - na koncu wskaznik na pusty element
  30. }
  31.  
  32. Data :: ~Data()
  33. {
  34. cout << "Deleted ";
  35. print();
  36. cout << endl;
  37. }
  38.  
  39. void Data :: print()
  40. {
  41. cout << " (" << data << ") ";
  42. }
  43.  
  44. /*
  45.   ________ ________ ________ ________ ________
  46.   | Data | ->| Data | ->| Data | | Data | ->| NULL |
  47.   List: |________| / |________| / |________| ... |________| / |________|
  48.   | *next |/ | *next |/ | *next | | *next |/ | |
  49.   |________| |________| |________| |________| |________|
  50.   first ->next ->next ... ->next ->next
  51.   ^
  52.   |
  53.   first->next->next
  54. */
  55.  
  56. struct List
  57. {
  58. // pierwszy element listy
  59. Data *first;
  60.  
  61. List();
  62. List(int); // List(int val)
  63. ~List();
  64. void push(int); // push(int val)
  65. int pop();
  66. void replace(int, int); // replace(int pos, int val)
  67. void insert(int, int); // insert(int pos, int val);
  68. void erase(int); // erase(int pos);
  69. void print();
  70. int size();
  71. void sort();
  72. };
  73.  
  74. // konstruktor tworzy pierwszy blok jako pusty
  75. List :: List()
  76. {
  77. cout << "New empty list" << endl;
  78. first = NULL;
  79. }
  80.  
  81. // konstruktor tworzy pierwszy element
  82. // kontruktor elementu tworzy wskaznik na kolejny pusty blok
  83. List :: List(int val)
  84. {
  85. cout << "New initialised list" << endl;
  86. first = new Data(val);
  87. }
  88.  
  89. // destruktor usuwa wszystkie elementy z listy
  90. List :: ~List()
  91. {
  92. cout << "List has been deleted" << endl;
  93. Data *temp = first;
  94. // Windows:
  95. Data *next = first->next;
  96.  
  97. // chodzimy po liscie
  98. // Linux:
  99. // while (temp)
  100. // Windows:
  101. while (temp->next)
  102. {
  103. // usuwamy obecny
  104. delete temp;
  105. // temp wskazuje na nastepny
  106. // Linux:
  107. // temp = temp->next;
  108. // Windows:
  109. temp = next;
  110. next = next->next;
  111. }
  112.  
  113. // pozostanie nam ostatni element do usuniecia
  114. delete temp;
  115. }
  116.  
  117. // wstawiamy nowy element na koncu listy,
  118. // pusty blok staje sie nowym elementem
  119. void List :: push(int val)
  120. {
  121. cout << "Pushed data into list (" << val << ")" << endl;
  122. // tworzy nowy element
  123. Data *data = new Data(val);
  124.  
  125. if (first == NULL)
  126. {
  127. /*
  128.   Gdy lista nie zawiera elementow
  129.   ________
  130.   | NULL |
  131.   List: |________|
  132.   | |
  133.   |________|
  134.   push(val)
  135.   Wstawiamy elment jako pierwszy
  136.   ________ ________
  137.   | Data | ->| NULL |
  138.   List: |__(val)_| / |________|
  139.   | *next |/ | |
  140.   |________| |________|
  141.   */
  142.  
  143. first = data;
  144. }
  145. else
  146. {
  147. /*
  148.   Gdy lista zawiera elementy, obchodzimy ja do konca i tam
  149.   wstawiamy nowy element
  150.   ________ ________ ________
  151.   | Data | ->| Data | ->| NULL |
  152.   List: |________| / |________| / |________|
  153.   | *next |/ | *next |/ | |
  154.   |________| |________| |________|
  155.   push(val)
  156.   Wstawiamy element na koniec
  157.   ________ ________ ________ ________
  158.   | Data | ->| Data | ->| Data | ->| NULL |
  159.   List: |________| / |________| / |__(val)_| / |________|
  160.   | *next |/ | *next |/ | *next |/ | |
  161.   |________| |________| |________| |________|
  162.   */
  163. // zaczynamy od pierwszego elementu
  164. Data *temp = first;
  165.  
  166. // chodzimy po liscie tak dlugo, az aktualny element
  167. // nie wskazuje na pusty blok
  168. while (temp->next)
  169. {
  170. temp = temp->next;
  171. }
  172.  
  173. // jak znajdzie taki blok to go uzupelnia
  174. temp->next = data;
  175. temp->next->next = NULL;
  176. }
  177. }
  178.  
  179. // Sciaga ostatni element z listy zastepujac go pustym blokiem
  180. int List :: pop()
  181. {
  182. /*
  183.   ________ ________ ________ ________
  184.   | Data | ->| Data | ->| Data | ->| NULL |
  185.   List: |________| / |________| / |__(val)_| / |________|
  186.   | *next |/ | *next |/ | *next |/ | |
  187.   |________| |________| |________| |________|
  188.   pop()
  189.   Sciagamy ostatni
  190.   ________ ________ ________
  191.   | Data | ->| Data | ->| NULL |
  192.   List: |________| / |________| / |________|
  193.   | *next |/ | *next |/ | |
  194.   |________| |________| |________|
  195.   */
  196. int ret_val;
  197.  
  198. if (first == NULL)
  199. {
  200. // Jezeli lista pusta to error
  201. cout << "Empty list! Cannot pop." << endl;
  202. ret_val = 0;
  203. }
  204. else if (!first->next)
  205. {
  206. // Jezeli lista 1-elementowa to usuwamy i zerujemy
  207. ret_val = first->data;
  208. delete first->next;
  209.  
  210. first = NULL;
  211. }
  212. else
  213. {
  214. // Jezeli lista >1-elementowa
  215. // poprzednik ostatniego
  216. Data *prev = first;
  217. // ostatni
  218. Data *data = first->next;
  219.  
  220. // chodzimy po liscie tak dlugo, az ostatni element
  221. // nie wskazuje na pusty blok
  222. while (data->next)
  223. {
  224. prev = prev->next;
  225. data = data->next;
  226. }
  227.  
  228. // zwracamy dane z ostatniego
  229. ret_val = data->data;
  230. // usuwamy ostatni
  231. delete data;
  232. // poprzednik wskazuje na pusty blok
  233. prev->next = NULL;
  234. }
  235.  
  236. cout << "Popped data from list (" << ret_val << ")" << endl;
  237.  
  238. return ret_val;
  239. }
  240.  
  241. void List :: replace(int pos, int val)
  242. {
  243. if (pos < 1 or pos > size())
  244. {
  245. cout << "Index out of range!" << endl;
  246. return;
  247. }
  248. else
  249. {
  250. cout << "Data at pos " << pos << " has been replaced by " << val << endl;
  251. int i = 1;
  252.  
  253. Data *temp = first;
  254.  
  255. while (i != pos)
  256. {
  257. i++;
  258. temp = temp->next;
  259. }
  260.  
  261. temp->data = val;
  262. }
  263. }
  264.  
  265. void List :: insert(int pos, int val)
  266. {
  267. // nowy element
  268. Data *data = new Data(val);
  269. if (pos < 1)
  270. {
  271. cout << "Index out of range!" << endl;
  272. return;
  273. }
  274. else if(!first)
  275. {
  276. cout << "Inserted " << val << " at pos " << pos << endl;
  277. // gdy lista pusta wstawiamy nowy element jako pierwszy
  278. first = data;
  279. }
  280. else
  281. {
  282. cout << "Inserted " << val << " at pos " << pos << endl;
  283. int i = 1;
  284.  
  285. if (!first)
  286. {
  287. // Jezeli lista pusta
  288. first = data;
  289. }
  290. else
  291. {
  292. // Jezeli lista zawiera elementy
  293. // poprzednik
  294. Data *prev = first;
  295. // nastepnik
  296. Data *next = first->next;
  297.  
  298. if (pos == 1)
  299. {
  300. first = data;
  301. data->next = prev;
  302. }
  303. else
  304. {
  305. while (i != pos)
  306. {
  307. i++;
  308. prev = prev->next;
  309. next = next->next;
  310. }
  311.  
  312. // poprzednik wskazuje na nowy element
  313. prev->next = data;
  314. // nowy element wskazuje na nastepnik;
  315. data->next = next;
  316. }
  317. }
  318. }
  319. }
  320.  
  321. void List :: erase(int pos)
  322. {
  323. if (pos < 1 or pos > size())
  324. {
  325. cout << "Index out of range!" << endl;
  326. return;
  327. }
  328. else
  329. {
  330. cout << "Erased data from pos " << pos << endl;
  331. int i = 1;
  332. Data *temp = first;
  333.  
  334. if (pos == 1)
  335. {
  336. // Jezeli usuwamy pierwszy element
  337. delete first;
  338. // pierwszy staje sie nastepnym
  339. first = temp->next;
  340. }
  341. else
  342. {
  343. // chodzimy po liscie
  344. while (temp)
  345. {
  346. if ((i + 1) == pos)
  347. {
  348. // Jezeli kolejny ma byc usuniety
  349. // zapisujemy sobie kolejny po usuwanym
  350. Data *temp2 = temp->next->next;
  351.  
  352. // usuwamy
  353. delete temp->next;
  354. // obecny wskazuje na kolejny po usuwanym
  355. temp->next = temp2;
  356.  
  357. break;
  358. }
  359.  
  360. temp = temp->next;
  361. i++;
  362. }
  363. }
  364. }
  365. }
  366.  
  367. void List :: print()
  368. {
  369. cout << "HEAD";
  370.  
  371. Data *temp = first;
  372.  
  373. while (temp)
  374. {
  375. cout << "->";
  376. temp->print();
  377. temp = temp->next;
  378. }
  379.  
  380. cout << endl;
  381. }
  382.  
  383. int List :: size()
  384. {
  385. int size = 0;
  386.  
  387. Data *temp = first;
  388.  
  389. while (temp)
  390. {
  391. size++;
  392. temp = temp->next;
  393. }
  394.  
  395. return size;
  396. }
  397.  
  398. void List :: sort()
  399. {
  400. if (!first or !first->next)
  401. {
  402. return;
  403. }
  404. else
  405. {
  406. Data *prev = first;
  407. for (int i = 0; i < size(); i++)
  408. {
  409. Data *next = prev->next;
  410. int temp;
  411.  
  412. for (int j = i; j < size() - 1; j++)
  413. {
  414. if (prev->data > next->data)
  415. {
  416. temp = prev->data;
  417. prev->data = next->data;
  418. next->data = temp;
  419. }
  420.  
  421. next = next->next;
  422. }
  423.  
  424. prev = prev->next;
  425. }
  426. }
  427. }
  428.  
  429. int main()
  430. {
  431. List *lista = new List();
  432.  
  433. lista->print();
  434.  
  435. lista->insert(1, -5);
  436. lista->print();
  437.  
  438. lista->push(-1);
  439. lista->push(2);
  440. lista->push(-2);
  441. lista->print();
  442.  
  443. lista->sort();
  444. lista->print();
  445.  
  446. lista->pop();
  447. lista->print();
  448.  
  449. lista->replace(0, 4);
  450. lista->replace(1, 4);
  451. lista->print();
  452.  
  453. lista->insert(1, 6);
  454. lista->print();
  455.  
  456. lista->erase(6);
  457. lista->erase(3);
  458. lista->print();
  459.  
  460. delete lista;
  461.  
  462. return 0;
  463. }
  464.  
Success #stdin #stdout 0s 3280KB
stdin
Standard input is empty
stdout
New empty list
HEAD
Inserted -5 at pos 1
HEAD-> (-5) 
Pushed data into list (-1)
Pushed data into list (2)
Pushed data into list (-2)
HEAD-> (-5) -> (-1) -> (2) -> (-2) 
HEAD-> (-5) -> (-2) -> (-1) -> (2) 
Deleted  (2) 
Popped data from list (2)
HEAD-> (-5) -> (-2) -> (-1) 
Index out of range!
Data at pos 1 has been replaced by 4
HEAD-> (4) -> (-2) -> (-1) 
Inserted 6 at pos 1
HEAD-> (6) -> (4) -> (-2) -> (-1) 
Index out of range!
Erased data from pos 3
Deleted  (-2) 
HEAD-> (6) -> (4) -> (-1) 
List has been deleted
Deleted  (6) 
Deleted  (4) 
Deleted  (-1)