Advertisement
Guest User

Untitled

a guest
Nov 1st, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. // ZADATAK JE MODIFICIRAN DA BI RADIO SA KONSTANTNIM REFERENCAMA, A I DODAN JE MAIN ZAJEDNO SA BENCHMARK TESTOM
  4.  
  5. using namespace std;
  6.  
  7. template<typename T>
  8. class Lista {
  9.  
  10. public:
  11.  
  12. Lista() {}
  13. Lista(Lista& rhs) {}
  14. virtual Lista& operator=(Lista& rhs) {}
  15. virtual ~Lista() {}
  16.  
  17. virtual int brojElemenata() const = 0;
  18. virtual T trenutni() const = 0;
  19. virtual T& trenutni() = 0;
  20. virtual bool prethodni() = 0;
  21. virtual bool sljedeci() = 0;
  22. virtual void pocetak() = 0;
  23. virtual void kraj() = 0;
  24. virtual void obrisi() = 0;
  25. virtual bool dodajIspred(const T&) = 0;
  26. virtual bool dodajIza(const T&) = 0;
  27. virtual T operator[](int) const = 0;
  28. virtual T& operator[](int) = 0;
  29.  
  30. };
  31.  
  32. template<typename T>
  33. class NizLista : public Lista<T> {
  34.  
  35. T** _niz, **_trenutni;// stavljeni dvostruki pokazivaci zbog problema sa konstantnim referencama
  36. int _brojE, _kapacitet;
  37. static const int INIT_NUM = 500;// Bilo 30, zakljuceno da je premalo, pa ispravljeno
  38.  
  39. public:
  40.  
  41. NizLista() : _niz(new T*[INIT_NUM]),
  42. _kapacitet(INIT_NUM), _trenutni(0), _brojE(0) {}
  43.  
  44. NizLista(const NizLista& rhs) {
  45. _kapacitet = rhs._kapacitet;
  46. _brojE = rhs._brojE;
  47. _niz = new T*[_kapacitet];
  48.  
  49. for(int i = 0; i < _brojE; i++)
  50. _niz[i] = new T(*rhs._niz[i]);
  51.  
  52. _trenutni = _niz + (rhs._trenutni - rhs._niz);
  53. }
  54.  
  55. virtual ~NizLista() { Unisti(); }
  56.  
  57.  
  58. virtual int brojElemenata() const { return _brojE; }
  59.  
  60. virtual T trenutni() const {
  61. if(_brojE == 0)
  62. throw "Empty list";
  63. return **_trenutni;
  64. }
  65.  
  66. virtual T& trenutni() {
  67. if(_brojE == 0)
  68. throw "Empty list";
  69.  
  70. return **_trenutni;
  71. }
  72.  
  73. virtual bool prethodni();
  74. virtual bool sljedeci();
  75. virtual void pocetak();
  76. virtual void kraj();
  77. virtual void obrisi();
  78. virtual bool dodajIspred(const T&);
  79. virtual bool dodajIza(const T&);
  80. virtual T operator[](int) const;
  81. virtual T& operator[](int);
  82.  
  83. virtual NizLista& operator=(NizLista& rhs);
  84.  
  85. void print() const;
  86.  
  87. private:
  88. void Unisti(); // FUNKCIJA DODATA KAO PRILAGODBA zbog dvostrukog pokazivaca, cisto da se ne kuca petlja u destruktoru
  89. void prosiriListu();
  90. };
  91.  
  92. template<typename T>
  93. void NizLista<T>::Unisti(){
  94. for(int i = 0 ; i<_brojE ; i++) delete _niz[i];
  95. delete[] _niz;
  96. }
  97.  
  98. template<typename T>
  99. NizLista<T>& NizLista<T>::operator=(NizLista<T>& rhs) {
  100.  
  101. Unisti();
  102. _kapacitet = rhs._kapacitet;
  103. _brojE = rhs._brojE;
  104. _niz = new T*[_kapacitet];
  105.  
  106. for(int i = 0; i < _brojE; i++)
  107. _niz[i] = new T(*rhs._niz[i]);
  108.  
  109. _trenutni = _niz + (rhs._trenutni - rhs._niz);
  110.  
  111. return *this;
  112. }
  113.  
  114. template<typename T>
  115. bool NizLista<T>::prethodni() {
  116. if(_trenutni == _niz || _brojE == 0)
  117. return false;
  118.  
  119. _trenutni--;
  120. return true;
  121. }
  122.  
  123. template<typename T>
  124. bool NizLista<T>::sljedeci() {
  125. if(_trenutni == _niz + (_brojE - 1) || _brojE == 0)
  126. return false;
  127.  
  128. _trenutni++;
  129. return true;
  130. }
  131.  
  132. template<typename T>
  133. void NizLista<T>::pocetak() {
  134. if(_brojE == 0)
  135. throw "Empty list";
  136.  
  137. _trenutni = _niz;
  138. }
  139.  
  140. template<typename T>
  141. void NizLista<T>::kraj() {
  142. if(_brojE == 0)
  143. throw "Empty list";
  144.  
  145. _trenutni = _niz + _brojE - 1;
  146. }
  147.  
  148. template<typename T>
  149. void NizLista<T>::obrisi() {
  150. if(_brojE == 0)
  151. throw "Empty list";
  152. delete *_trenutni;
  153. if(_trenutni == _niz + _brojE - 1) _trenutni--;
  154.  
  155. else
  156. for(T** i = _trenutni; i < _niz + _brojE - 1; i++)
  157. *i = *(i+1);
  158.  
  159. _brojE--;
  160.  
  161. }
  162.  
  163. template<typename T>
  164. bool NizLista<T>::dodajIza(const T& val) {
  165. if(_brojE == 0) {
  166. *_niz = new T(val);
  167. _trenutni = _niz;
  168. _brojE++;
  169. return true;
  170. }
  171.  
  172. if(_brojE == _kapacitet)
  173. prosiriListu();
  174.  
  175. for(int i = _brojE; i > _trenutni - _niz + 1; i--)
  176. _niz[i] = _niz[i - 1];
  177.  
  178. *(_trenutni + 1) =new T(val);
  179.  
  180. _brojE++;
  181. return true;
  182. }
  183.  
  184. template<typename T>
  185. bool NizLista<T>::dodajIspred(const T& val) {
  186. if(_brojE == 0) {
  187. *_niz = new T(val);
  188. _trenutni = _niz;
  189. _brojE++;
  190. return true;
  191. }
  192.  
  193. if(_brojE == _kapacitet)
  194. prosiriListu();
  195.  
  196. for(int i = _brojE; i >= _trenutni - _niz + 1; i--)
  197. _niz[i] = _niz[i - 1];
  198.  
  199. *_trenutni = new T(val);
  200. _trenutni++;
  201. _brojE++;
  202. return true;
  203. }
  204.  
  205. template<typename T>
  206. void NizLista<T>::prosiriListu() {
  207. _kapacitet *= 2;
  208. T** noviNiz = new T*[_kapacitet];
  209. for(int i = 0; i < _brojE; i++)
  210. noviNiz[i] = _niz[i];
  211. _trenutni = noviNiz + (_trenutni - _niz);
  212. delete[] _niz;
  213. _niz = noviNiz;
  214. }
  215.  
  216. template<typename T>
  217. T NizLista<T>::operator[](int at) const {
  218. if(at < 0 || at >= _brojE)
  219. throw "Index out of range";
  220.  
  221. return *_niz[at];
  222. }
  223.  
  224. template<typename T>
  225. T& NizLista<T>::operator[](int at) {
  226. if(at < 0 || at >= _brojE)
  227. throw "Index out of range";
  228.  
  229. return *_niz[at];
  230. }
  231.  
  232. template<typename T>
  233. void NizLista<T>::print() const {
  234. for(int i = 0; i < _brojE; i++)
  235. cout << *_niz[i] << " ";
  236.  
  237. cout << endl;
  238. }
  239.  
  240.  
  241. template<typename T>
  242. class Node {
  243.  
  244. public:
  245.  
  246. T _val;
  247. Node* _next;
  248.  
  249. Node() {
  250. _val = 0;
  251. _next = 0;
  252. }
  253.  
  254. Node(const T& val) {
  255. _val = val;
  256. _next = 0;
  257. }
  258.  
  259. static Node* getNode(const T& val) {
  260. return new Node(val);
  261. }
  262.  
  263. };
  264.  
  265. template<typename T>
  266. class JednostrukaLista : public Lista<T> {
  267.  
  268. Node<T> *_first, *_current,
  269. *_last;
  270. int _brojE;
  271.  
  272. public:
  273.  
  274. JednostrukaLista() : _first(0), _current(0),
  275. _last(0), _brojE(0) {}
  276.  
  277. JednostrukaLista(const JednostrukaLista&);
  278. JednostrukaLista& operator=(const JednostrukaLista&);
  279.  
  280. ~JednostrukaLista() { deleteAll(); }
  281.  
  282. virtual int brojElemenata() const { return _brojE; }
  283.  
  284. virtual T trenutni() const;
  285. virtual T& trenutni();
  286. virtual bool prethodni();
  287. virtual bool sljedeci();
  288. virtual void pocetak();
  289. virtual void kraj();
  290. virtual void obrisi();
  291. virtual bool dodajIza(const T&);
  292. virtual bool dodajIspred(const T&);
  293. virtual T operator[](int) const;
  294. virtual T& operator[](int);
  295.  
  296. virtual void print() const;
  297.  
  298. private:
  299.  
  300. void deleteAll();
  301. };
  302.  
  303. template<typename T>
  304. JednostrukaLista<T>::JednostrukaLista(const JednostrukaLista<T>& rhs) {
  305. _brojE = rhs._brojE;
  306. _first = Node<T>::getNode(rhs._first->_val);
  307. Node<T> *j = _first, *i = rhs._first->_next;
  308. while(true) {
  309. if(i == rhs._current)
  310. _current = j;
  311. j->_next = Node<T>::getNode(i->_val);
  312. j = j->_next;
  313. if(i->_next == 0)
  314. break;
  315. i = i->_next;
  316. }
  317.  
  318. _last = j;
  319. }
  320.  
  321. template<typename T>
  322. JednostrukaLista<T>& JednostrukaLista<T>::operator=(const JednostrukaLista<T>& rhs) {
  323.  
  324. deleteAll();
  325. _brojE = rhs._brojE;
  326. _first = Node<T>::getNode(rhs._first->_val);
  327. Node<T> *j = _first, *i = rhs._first->_next;
  328. while(true) {
  329. if(i == rhs._current)
  330. _current = j;
  331. j->_next = Node<T>::getNode(i->_val);
  332. j = j->_next;
  333. if(i->_next == 0)
  334. break;
  335. i = i->_next;
  336. }
  337.  
  338. _last = j;
  339.  
  340. return *this;
  341. }
  342.  
  343. template<typename T>
  344. T JednostrukaLista<T>::trenutni() const {
  345. if(_brojE == 0)
  346. throw "Empty list";
  347.  
  348. return _current->_val;
  349. }
  350.  
  351. template<typename T>
  352. T& JednostrukaLista<T>::trenutni() {
  353. if(_brojE == 0)
  354. throw "Empty list";
  355.  
  356. return _current->_val;
  357. }
  358.  
  359. template<typename T>
  360. bool JednostrukaLista<T>::sljedeci() {
  361. if(_current == _last)
  362. return false;
  363.  
  364. _current = _current->_next;
  365. return true;
  366. }
  367.  
  368. template<typename T>
  369. bool JednostrukaLista<T>::prethodni() {
  370. if(_current == _first)
  371. return false;
  372.  
  373. Node<T>* i;
  374. for(i = _first; i->_next != _current; i = i->_next);
  375.  
  376. _current = i;
  377. return true;
  378. }
  379.  
  380. template<typename T>
  381. void JednostrukaLista<T>::pocetak() {
  382. if(_brojE == 0)
  383. throw "Empty list";
  384.  
  385. _current = _first;
  386. }
  387.  
  388. template<typename T>
  389. void JednostrukaLista<T>::kraj() {
  390. if(_brojE == 0)
  391. throw "Empty list";
  392.  
  393. _current = _last;
  394. }
  395.  
  396. template<typename T>
  397. void JednostrukaLista<T>::obrisi() {
  398. if(_brojE == 0)
  399. throw "Empty list";
  400. else if(_brojE == 1) {
  401. delete _first;
  402. _first = 0;
  403. _current = 0;
  404. _last = 0;
  405. } else if(_current == _first) {
  406. Node<T>* oldCurrent = _current;
  407. _first->_next = _current->_next;
  408. _current = _first->_next;
  409. _first = _current;
  410. delete oldCurrent;
  411. } else if(_current == _last) {
  412. prethodni();
  413. _last = _current;
  414. delete _current->_next;
  415. } else {
  416. prethodni();
  417. Node<T>* oldCurrent = _current->_next;
  418. _current->_next = _current->_next->_next;
  419. delete oldCurrent;
  420. _current = _current->_next;
  421. }
  422.  
  423. _brojE--;
  424. }
  425.  
  426. template<typename T>
  427. bool JednostrukaLista<T>::dodajIza(const T& val) {
  428. if(_brojE == 0) {
  429. _first = Node<T>::getNode(val);
  430. _last = _first;
  431. _current = _first;
  432. _brojE++;
  433. return true;
  434. }
  435.  
  436. bool isLast = (_current == _last);
  437.  
  438. Node<T>* oldNext = _current->_next;
  439. _current->_next = Node<T>::getNode(val);
  440. _current->_next->_next = oldNext;
  441. _brojE++;
  442.  
  443. if(isLast)
  444. _last = _current->_next;
  445.  
  446. return true;
  447. }
  448.  
  449. template<typename T>
  450. bool JednostrukaLista<T>::dodajIspred(const T& val) {
  451. if(_brojE == 0) {
  452. _first = Node<T>::getNode(val);
  453. _last = _first;
  454. _current = _first;
  455. _brojE++;
  456. return true;
  457. }
  458.  
  459. if(_first == _current) {
  460. _first = Node<T>::getNode(val);
  461. _first->_next = _current;
  462. _brojE++;
  463. return true;
  464. }
  465.  
  466. Node<T>* oldCurrent = _current;
  467. prethodni();
  468. _current->_next = Node<T>::getNode(val);
  469. _current->_next->_next = oldCurrent;
  470. _current = oldCurrent;
  471. _brojE++;
  472. return true;
  473. }
  474.  
  475. template<typename T>
  476. void JednostrukaLista<T>::print() const {
  477. Node<T>* i = _first;
  478. while(true) {
  479. cout << i->_val << " ";
  480. if(i == _last)
  481. break;
  482. i = i->_next;
  483. }
  484.  
  485. cout << endl;
  486. }
  487.  
  488. template<typename T>
  489. void JednostrukaLista<T>::deleteAll() {
  490. while(_brojE != 0)
  491. obrisi();
  492. }
  493.  
  494. template<typename T>
  495. T JednostrukaLista<T>::operator[](int at) const {
  496. if(at < 0 || at >= _brojE)
  497. throw "Index out of range";
  498.  
  499. Node<T> *i = _first;
  500. for(int j = 0; j < at; j++)
  501. i = i->_next;
  502.  
  503. return i->_val;
  504. }
  505.  
  506. template<typename T>
  507. T& JednostrukaLista<T>::operator[](int at) {
  508. if(at < 0 || at >= _brojE)
  509. throw "Index out of range";
  510.  
  511. Node<T>* i = _first;
  512. for(int j = 0; j < at; j++)
  513. i = i->_next;
  514.  
  515. return i->_val;
  516. }
  517.  
  518. template<typename T>
  519. class Node2{
  520.  
  521. public:
  522. Node2* _previous;
  523. Node2* _next;
  524. T _value;
  525.  
  526.  
  527. Node2() : _previous(0), _next(0), _value(0) {}
  528. Node2(const T& value){
  529. _value = value;
  530. _previous = 0;
  531. _next = 0;
  532. }
  533.  
  534. static Node2* GetNode(T value){return new Node2(value);}
  535.  
  536. };
  537.  
  538.  
  539. template<typename T>
  540. class DvostrukaLista : public Lista<T>{
  541.  
  542. int _brojE;
  543. Node2<T> *_head, *_tail, *_current;
  544.  
  545. public:
  546.  
  547. DvostrukaLista() : _head(0), _tail(0), _current(0), _brojE(0) {}
  548. DvostrukaLista(const DvostrukaLista&);
  549. DvostrukaLista& operator=(const DvostrukaLista&);
  550. ~DvostrukaLista() { Ponisti();}
  551.  
  552. //METODE
  553.  
  554. int brojElemenata() const { return _brojE;}
  555. T trenutni() const;
  556. T& trenutni();
  557. bool prethodni();
  558. bool sljedeci();
  559. void pocetak();
  560. void kraj();
  561. void obrisi();
  562. bool dodajIspred(const T&);
  563. bool dodajIza(const T&);
  564. T operator[](int pozicija) const;
  565. T& operator[](int pozicija);
  566. void Isprintaj() const;
  567.  
  568. private:
  569.  
  570. void Ponisti();
  571.  
  572.  
  573. };
  574.  
  575. template<typename T>
  576. T DvostrukaLista<T>::trenutni() const {
  577. if(_brojE == 0) throw "Lista je prazna";
  578. return _current -> _value;
  579. }
  580.  
  581. template<typename T>
  582. T& DvostrukaLista<T>::trenutni(){
  583. if(_brojE == 0) throw "Lista je prazna";
  584. return _current->_value;
  585. }
  586.  
  587. template<typename T>
  588. bool DvostrukaLista<T>::prethodni(){
  589. if(_brojE == 0) throw "Lista je prazna";
  590. if(_current == _head) return false;
  591. _current = _current -> _previous;
  592. return true;
  593. }
  594.  
  595. template<typename T>
  596. bool DvostrukaLista<T>::sljedeci(){
  597. if(_brojE == 0) throw "Lista je prazna";
  598. if(_current == _tail) return false;
  599. _current = _current ->_next;
  600. return true;
  601. }
  602.  
  603. template<typename T>
  604. void DvostrukaLista<T>::pocetak(){
  605.  
  606. if(_brojE == 0) throw "Lista je prazna";
  607. _current = _head;
  608. }
  609.  
  610. template<typename T>
  611. void DvostrukaLista<T>::kraj(){
  612.  
  613. if(_brojE == 0) throw "Lista je prazna";
  614. _current = _tail;
  615. }
  616.  
  617. template<typename T>
  618. void DvostrukaLista<T>::obrisi(){
  619.  
  620. if( _brojE == 0) throw "Lista je prazna";
  621. if(_brojE == 1){
  622.  
  623. Node2<T>* oldCurrent = _current;
  624. _current = 0;
  625. _head = 0;
  626. _tail = 0;
  627. delete oldCurrent;
  628. _brojE--;
  629. }
  630. else if( _current == _head){
  631.  
  632. Node2<T>* oldHead = _head;
  633. _current = _current->_next;
  634. _head = _current;
  635. _head->_previous = 0;
  636. delete oldHead;
  637. _brojE--;
  638. }
  639. else if(_current == _tail){
  640.  
  641. Node2<T>* oldTail = _tail;
  642. _current = _current -> _previous;
  643. _tail = _current;
  644. _tail -> _next = 0;
  645. delete oldTail;
  646. _brojE--;
  647. }
  648. else {
  649.  
  650. Node2<T>* oldCurrent = _current;
  651. _current->_previous -> _next = oldCurrent -> _next;
  652. _current -> _next -> _previous = _current ->_previous;
  653. _current = _current -> _next;
  654. delete oldCurrent;
  655. _brojE--;
  656. }
  657.  
  658. }
  659.  
  660. template<typename T>
  661. bool DvostrukaLista<T>::dodajIza(const T& element){
  662.  
  663. if(_brojE == 0){
  664.  
  665. _head = Node2<T>::GetNode(element);
  666. _current = _head;
  667. _tail = _head;
  668. _brojE++;
  669. return true;
  670. }
  671. else if(_brojE == 1){
  672.  
  673.  
  674. _current -> _next = Node2<T>::GetNode(element);
  675. _tail = _current ->_next;
  676. _tail -> _previous = _current;
  677. _tail->_next = 0;
  678. _brojE++;
  679. return true;
  680. }
  681. else if(_current == _tail){
  682.  
  683. _tail -> _next = Node2<T>::GetNode(element);
  684. _tail = _tail->_next;
  685. _tail -> _previous = _current;
  686. _tail->_next = 0;
  687. _brojE++;
  688. }
  689. else{
  690.  
  691. Node2<T>* novi = Node2<T>::GetNode(element);
  692. Node2<T>* oldNext = _current->_next;
  693. _current -> _next = novi;
  694. novi->_next = oldNext;
  695. oldNext->_previous = novi;
  696. novi->_previous= _current;
  697.  
  698. _brojE++;
  699. return true;
  700. }
  701.  
  702. }
  703.  
  704. template<typename T>
  705. bool DvostrukaLista<T>::dodajIspred(const T& element){
  706.  
  707. if(_brojE == 0){
  708.  
  709. _head = Node2<T>::GetNode(element);
  710. _head->_previous = 0;
  711. _current = _head;
  712. _tail = _head;
  713. _tail->_next = 0;
  714. _brojE++;
  715. return true;
  716. }
  717. else if(_brojE == 1){
  718.  
  719.  
  720. _current -> _previous = Node2<T>::GetNode(element);
  721. _head = _current ->_previous;
  722. _head -> _next = _current;
  723. _head->_previous = 0;
  724. _brojE++;
  725. return true;
  726. } else if(_current == _head){
  727.  
  728. _current -> _previous = Node2<T>::GetNode(element);
  729. _head = _current->_previous;
  730. _head->_next = _current;
  731. _head->_previous = 0;
  732. _brojE++;
  733. }
  734. else{
  735.  
  736. Node2<T>* novi = Node2<T>::GetNode(element);
  737.  
  738. _current->_previous->_next=novi;
  739. novi->_previous=_current->_previous;
  740. novi->_next=_current;
  741. _current->_previous=novi;
  742. _brojE++;
  743. return true;
  744.  
  745. }
  746.  
  747. }
  748.  
  749. template<typename T>
  750. T DvostrukaLista<T>::operator[](int pozicija) const{
  751. if(_brojE == 0) throw "Lista je prazna";
  752. Node2<T>* j = _head;
  753. for(int i = 0; i < pozicija ; i++) j=j->_next;
  754. return j->_value;
  755. }
  756.  
  757. template<typename T>
  758. T& DvostrukaLista<T>::operator[](int pozicija){
  759. if(_brojE == 0) throw "Lista je prazna";
  760. Node2<T>* j = _head;
  761. for(int i = 0; i < pozicija ; i++) j=j->_next;
  762. return j->_value;
  763. }
  764.  
  765. template<typename T>
  766. void DvostrukaLista<T>::Isprintaj() const{
  767.  
  768. Node2<T>* j = _head;
  769.  
  770. for(int i = 0; i < _brojE; i++){ cout << j->_value << " "; j = j->_next;}
  771. cout << endl;
  772. }
  773.  
  774. template<typename T>
  775. void DvostrukaLista<T>::Ponisti(){
  776.  
  777. while(_brojE != 0) obrisi();
  778.  
  779. }
  780.  
  781. template<typename T>
  782. DvostrukaLista<T>::DvostrukaLista(const DvostrukaLista<T>& rhs){
  783.  
  784. _brojE = rhs._brojE;
  785. _head = Node2<T>::GetNode(rhs._head->_value);
  786. Node2<T> *j = _head, *i = rhs._head->_next, *temporary;
  787. while(true) {
  788. j->_next = Node2<T>::GetNode(i->_value);
  789. if(i == rhs._current)
  790. _current = j->_next;
  791. temporary = j;
  792. j = j->_next;
  793. j->_previous = temporary;
  794. if(i->_next == 0) break;
  795. i = i->_next;
  796. }
  797.  
  798. _tail = j;
  799. _tail->_next = 0;
  800.  
  801. }
  802.  
  803.  
  804.  
  805. template<typename T>
  806. DvostrukaLista<T>& DvostrukaLista<T>::operator=(const DvostrukaLista<T>& rhs){
  807.  
  808. Ponisti();
  809. _brojE = rhs._brojE;
  810. _head = Node2<T>::GetNode(rhs._head->_value);
  811. Node2<T> *j = _head, *i = rhs._head->_next, *temporary;
  812. while(true) {
  813. j->_next = Node2<T>::GetNode(i->_value);
  814. if(i == rhs._current)
  815. _current = j->_next;
  816. temporary = j;
  817. j = j->_next;
  818. j->_previous = temporary;
  819. if(i->_next == 0) break;
  820. i = i->_next;
  821. }
  822.  
  823. _tail = j;
  824. _tail->_next = 0;
  825.  
  826. return *this;
  827. }
  828.  
  829.  
  830.  
  831. int main() {
  832. cout << "==========NIZLISTA==========";
  833. for(int i = 0; i<4; i++) cout << endl;
  834. NizLista<int> nizlista;
  835.  
  836. for(int i = 1; i <= 5 ;i++) {
  837.  
  838. nizlista.dodajIza(i);
  839. nizlista.sljedeci();
  840. }
  841.  
  842. nizlista.print();
  843. cout << endl;
  844.  
  845. cout << nizlista.trenutni() << endl;
  846. nizlista.sljedeci();
  847. cout << nizlista.trenutni() << endl;
  848. nizlista.prethodni();
  849. cout << nizlista.trenutni() << endl;
  850. nizlista.pocetak();
  851. cout << nizlista.trenutni() << endl;
  852. nizlista.kraj();
  853. cout << nizlista.trenutni() << endl;
  854. cout << nizlista.brojElemenata() << endl;
  855.  
  856. nizlista.sljedeci();
  857. nizlista.sljedeci();
  858.  
  859. nizlista.obrisi();
  860. nizlista.print();
  861.  
  862. cout << endl << nizlista[2];
  863. cout << endl;
  864.  
  865. NizLista<int> nizlista2(nizlista);
  866.  
  867. nizlista2.print();
  868.  
  869. cout << endl;
  870.  
  871. NizLista<int> nizlista3 = nizlista2;
  872. nizlista3.print();
  873.  
  874. cout << "==========DVOSTRUKA LISTA==========";
  875. for(int i = 0; i<4; i++) cout << endl;
  876.  
  877. DvostrukaLista<int> lista;
  878.  
  879. for(int i = 1; i <= 5 ;i++) lista.dodajIspred(i);
  880. lista.Isprintaj();
  881. cout<< endl;
  882. cout << lista.trenutni() << endl;
  883. lista.prethodni();
  884. cout << lista.trenutni() << endl;
  885. lista.prethodni();
  886. lista.prethodni();
  887. cout<< lista.trenutni() << endl;
  888. lista.sljedeci();
  889. cout << lista.trenutni() << endl;
  890.  
  891. lista.pocetak();
  892. cout<< lista.trenutni() << endl;
  893.  
  894. lista.kraj();
  895. cout << lista.trenutni() << endl;
  896.  
  897. lista.dodajIspred(8);
  898. lista.Isprintaj();
  899. lista.dodajIza(9);
  900. lista.Isprintaj();
  901. lista.kraj();
  902. lista.obrisi();
  903. lista.Isprintaj();
  904. cout << lista.trenutni();
  905. cout << endl;
  906. DvostrukaLista<int> lista2(lista);
  907. cout << endl;
  908.  
  909. lista2.Isprintaj();
  910. cout << lista2.trenutni() << endl << endl;
  911. lista2.sljedeci();
  912.  
  913. cout << lista2.trenutni() << endl;
  914. lista2.prethodni();
  915. cout << lista2.trenutni() << endl;
  916.  
  917. DvostrukaLista<int> lista3 = lista2;
  918.  
  919. cout << endl;
  920. lista3.Isprintaj();
  921. cout << endl;
  922.  
  923.  
  924. cout << "==========JEDNOSTRUKA LISTA==========";
  925. for(int i = 0; i<4; i++) cout << endl;
  926.  
  927. JednostrukaLista<int> listax;
  928.  
  929. for(int i = 1; i <= 5 ;i++) listax.dodajIspred(i);
  930. listax.print();
  931. cout<< endl;
  932. cout << listax.trenutni() << endl;
  933. listax.prethodni();
  934. cout << listax.trenutni() << endl;
  935. listax.prethodni();
  936. listax.prethodni();
  937. cout<< listax.trenutni() << endl;
  938. listax.sljedeci();
  939. cout << listax.trenutni() << endl;
  940.  
  941. listax.pocetak();
  942. cout<< listax.trenutni() << endl;
  943.  
  944. listax.kraj();
  945. cout << listax.trenutni() << endl;
  946.  
  947. listax.dodajIspred(8);
  948. listax.print();
  949. listax.dodajIza(9);
  950. listax.print();
  951. listax.kraj();
  952. listax.obrisi();
  953. listax.print();
  954. cout << listax.trenutni();
  955. cout << endl;
  956. JednostrukaLista<int> lista2x(listax);
  957. cout << endl;
  958.  
  959. lista2x.print();
  960. cout << lista2x.trenutni() << endl << endl;
  961. lista2x.sljedeci();
  962.  
  963. cout << lista2x.trenutni() << endl;
  964. lista2x.prethodni();
  965. cout << lista2x.trenutni() << endl;
  966.  
  967. JednostrukaLista<int> lista3x = lista2x;
  968.  
  969. cout << endl;
  970. lista3x.print();
  971. cout << endl;
  972.  
  973. cout << "==========BENCHMARK TESTIRANJE PERFORMANSI==========";
  974. for(int i = 0; i<5; i++) cout << endl;
  975. JednostrukaLista<int> hljeb;
  976. for(int i = 1 ; i<=100000 ; i++){
  977.  
  978. hljeb.dodajIza(1);
  979.  
  980. }
  981.  
  982. clock_t vrijeme1 = clock();
  983.  
  984. hljeb.prethodni();
  985.  
  986. clock_t vrijeme2 = clock();
  987.  
  988. int ukvrijeme = (vrijeme2 - vrijeme1) / (CLOCKS_PER_SEC / 1000);
  989. cout << "Vrijeme izvrsenja metode prethodni za JEDNOSTRUKU listu je: " << ukvrijeme << " ms." << endl;
  990.  
  991. DvostrukaLista<int> pogaca;
  992. for(int i = 1 ; i<=1000000 ; i++){
  993.  
  994. pogaca.dodajIza(1);
  995.  
  996. }
  997.  
  998.  
  999.  
  1000. clock_t vrijeme12 = clock();
  1001.  
  1002. pogaca.prethodni();
  1003.  
  1004. clock_t vrijeme21 = clock();
  1005.  
  1006. int ukvrijeme1 = (vrijeme21 - vrijeme12) / (CLOCKS_PER_SEC / 1000);
  1007. cout << "Vrijeme izvrsenja metode prethodni za DVOSTRUKU listu je: " << ukvrijeme1 << " ms." << endl;
  1008.  
  1009.  
  1010.  
  1011.  
  1012. return 0;
  1013.  
  1014. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement