Guest User

Untitled

a guest
May 27th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template < class T >
  5. int compare( T jeden, T dwa)
  6. {
  7. if(jeden == dwa) return 0;
  8. if(jeden > dwa) return 1;
  9. else return -1;
  10. }
  11.  
  12. template < class T >
  13. class Lista
  14. {
  15. public:
  16. struct wezel
  17. {
  18. T wartosc;
  19. wezel *next;
  20. };
  21.  
  22. wezel * pierwszy;
  23. int dlugosc;
  24. wezel * wskazywany;
  25. Lista(){ pierwszy = wskazywany = NULL; dlugosc = 0; }
  26.  
  27.  
  28. class Iterator
  29. {
  30. public:
  31. wezel* element;
  32. Iterator& operator++() { Iterator n = *this; n.element = n.element->next; *this = n; return n; }
  33. wezel& operator*() { return *(this->element); }
  34. bool operator==(const Iterator& i2)
  35. { return this->element->next == i2.element->next; }
  36. bool operator!=(const Iterator& i2)
  37. { return this->element->next != i2.element->next; }
  38. };
  39.  
  40. Iterator iter;
  41. Iterator& Begin() { Iterator n; n.element = pierwszy; return n; }
  42. Iterator& End() { idzNaKoniec(); Iterator n; n.element = wskazywany; return n; }
  43.  
  44. void idzO(int ile)
  45. {
  46. for(int i = 0; (i < ile) && (wskazywany->next != NULL); i++)
  47. wskazywany = wskazywany->next;
  48. }
  49. void idzNaKoniec()
  50. {
  51. idzNaPoczatek();
  52. while(wskazywany->next)
  53. wskazywany = wskazywany->next;
  54. return;
  55. }
  56. void idzNaPoczatek()
  57. {
  58. wskazywany = pierwszy;
  59. }
  60. void dodaj(T war)
  61. {
  62. wezel *nowy = new wezel;
  63. nowy->wartosc = war;
  64. idzNaPoczatek();
  65. if(wskazywany == NULL)
  66. {
  67. nowy->next = NULL;
  68. pierwszy = nowy;
  69. }
  70. else if(compare(war, wskazywany->wartosc) == -1){
  71. nowy->next = pierwszy;
  72. pierwszy = nowy;
  73. }
  74. else
  75. {
  76. while(compare(war, wskazywany->wartosc) == 1 && wskazywany->next != NULL)
  77. {
  78. idzO(1);
  79. }
  80. nowy->next = wskazywany->next;
  81. wskazywany->next = nowy;
  82. }
  83. dlugosc++;
  84. return;
  85. }
  86. void print()
  87. {
  88. idzNaPoczatek();
  89. int i = 0;
  90. while(i < dlugosc)
  91. {
  92. cout << wskazywany->wartosc << " ";
  93. wskazywany = wskazywany->next;
  94. i++;
  95. }
  96. return;
  97. }
  98. ~Lista()
  99. {
  100. for(int i = 0; i < dlugosc; i++)
  101. {
  102. idzNaPoczatek();
  103. while(wskazywany->next)
  104. {
  105. wskazywany = wskazywany->next;
  106. }
  107. wskazywany = NULL;
  108. delete wskazywany;
  109. cout << "usuwam \n";
  110. }
  111. }
  112. };
  113.  
  114. int main()
  115. {
  116. Lista<int> *lista = new Lista<int>;
  117. for(int i = 3; i < 10; i++)
  118. lista->dodaj(i);
  119. lista->dodaj(2);
  120. lista->dodaj(32);
  121. lista->dodaj(6);
  122. lista->print();
  123. lista->idzNaKoniec();
  124. lista->iter = lista->Begin();
  125. cout << "\n" << lista->iter.element->wartosc << " ";
  126. lista->iter = lista->End();
  127. cout << lista->iter.element->wartosc << endl;
  128.  
  129. delete lista;
  130. system("pause");
  131. return 0;
  132. }
Add Comment
Please, Sign In to add comment