Advertisement
Guest User

Untitled

a guest
Nov 28th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. /* HEADER */
  2.  
  3. #ifndef OTE_INTLISTA_H
  4. #define OTE_INTLISTA_H
  5. #include <vector>
  6. #include <iostream>
  7.  
  8. namespace otecpp_intlista
  9. {
  10. class IntSolmu
  11. {
  12. friend class IntLista;
  13. int arvo_;
  14. IntSolmu *seur_;
  15. public:
  16. IntSolmu(int arvo = 0, IntSolmu *seur = NULL);
  17. int arvo() const;
  18. IntSolmu* seur() const;
  19. void seur(IntSolmu *s);
  20. };
  21.  
  22. std::ostream & operator<<(std::ostream &virta, const IntSolmu *s);
  23.  
  24. class IntLista
  25. {
  26. public:
  27. typedef size_t size_type;
  28.  
  29. private:
  30. IntSolmu *paa_;
  31. size_type koko_;
  32.  
  33. public:
  34. IntLista();
  35. IntLista(const std::vector<int> &t);
  36. ~IntLista();
  37. IntLista(const IntLista &il2);
  38. IntLista & operator=(const IntLista &il2);
  39. void lisaaEteen(int arvo);
  40. void poistaEdesta();
  41. IntSolmu * paa() const;
  42. size_type koko() const;
  43. int & operator[](size_type i);
  44. const int & operator[](size_type i) const;
  45. operator std::vector<int>() const;
  46. };
  47. }
  48. #endif
  49.  
  50.  
  51. /* CPP */
  52.  
  53. #include "intlista.h"
  54.  
  55. using namespace std;
  56. namespace otecpp_intlista {
  57. IntSolmu * IntLista::paa() const {
  58. return IntLista::paa_;
  59. }
  60. IntLista::size_type IntLista::koko() const
  61. {
  62. return koko_;
  63. }
  64. void IntLista::poistaEdesta() {
  65. if (koko_ > 0)
  66. {
  67. IntSolmu * tmp = paa_;
  68. paa_ = paa_->seur();
  69. delete tmp;
  70. koko_ -= 1;
  71. }
  72. }
  73. void IntLista::lisaaEteen(int arvo) {
  74. paa_ = new IntSolmu(arvo, paa_);
  75. koko_ += 1;
  76. }
  77. IntLista & IntLista::operator=(const IntLista &il2) {
  78. if (this != &il2)
  79. {
  80. IntSolmu *s = paa_;
  81. while (s != NULL)
  82. {
  83. IntSolmu *tmp = s->seur();
  84. delete s;
  85. s = tmp;
  86. }
  87. koko_ = 0;
  88. if (il2.koko_ > 0)
  89. {
  90. paa_ = new IntSolmu(il2.paa_->arvo(), NULL);
  91. IntSolmu *s = paa_;
  92. koko_ = 1;
  93. for (IntSolmu * s2 = il2.paa_->seur(); s2 != NULL; s2 = s2->seur())
  94. {
  95. IntSolmu *tmp = new IntSolmu(s2->arvo(), NULL);
  96. s->seur(tmp);
  97. s = tmp;
  98. koko_ += 1;
  99. }
  100. }
  101. }
  102. return *this;
  103. }
  104. IntLista::IntLista(const IntLista &il2) : paa_(NULL), koko_(0)
  105. {
  106. if (il2.koko_ > 0)
  107. {
  108. paa_ = new IntSolmu(il2.paa_->arvo(), NULL);
  109. IntSolmu *s = paa_;
  110. koko_ = 1;
  111. for (IntSolmu * s2 = il2.paa_->seur(); s2 != NULL; s2 = s2->seur())
  112. {
  113. IntSolmu *tmp = new IntSolmu(s2->arvo(), NULL);
  114. s->seur(tmp);
  115. s = tmp;
  116. koko_ += 1;
  117. }
  118. }
  119. }
  120. IntLista::IntLista()
  121. : paa_(NULL), koko_(0)
  122. {
  123. }
  124. IntLista::~IntLista() {
  125.  
  126. IntSolmu *s = paa_;
  127. while (s != NULL)
  128. {
  129. IntSolmu *tmp = s->seur();
  130. delete s;
  131. s = tmp;
  132. }
  133.  
  134. }
  135. IntLista::IntLista(const vector<int> &t) {
  136. koko_ = 0;
  137. paa_ = NULL;
  138. if (t.size() > 0) {
  139. for (int i = t.size() - 1; i >= 0; i--) {
  140. this->lisaaEteen(t[i]);
  141. }
  142.  
  143. }
  144. }
  145. IntSolmu::IntSolmu(int arvo, IntSolmu *seur)
  146. : arvo_(arvo), seur_(seur)
  147. {
  148. }
  149. int IntSolmu::arvo() const
  150. {
  151. return arvo_;
  152. }
  153. IntSolmu* IntSolmu::seur() const
  154. {
  155. return seur_;
  156. }
  157. void IntSolmu::seur(IntSolmu *s)
  158. {
  159. seur_ = s;
  160. }
  161. ostream &operator<<(ostream &virta, const IntLista &lista) {
  162. int luku = 0;
  163. IntSolmu *s = lista.paa();
  164. while (s != NULL) {
  165. IntSolmu *tmp = s->seur();
  166. if (luku == 0) {
  167. virta << s->arvo();
  168. luku = 1;
  169. }
  170. else {
  171. virta << " " << s->arvo();
  172. }
  173. s = tmp;
  174. }
  175. return virta;
  176. }
  177.  
  178. ostream & operator<<(ostream &virta, const IntSolmu *s) {
  179. if (s != NULL) {
  180. virta << s->arvo();
  181. }
  182. else {
  183. virta << "NULL";
  184. }
  185. return virta;
  186. }
  187.  
  188. int & IntLista::operator[](size_type i) {
  189. IntLista::size_type luku = 0;
  190. IntSolmu *s = paa_;
  191. while (true) {
  192. IntSolmu *tmp = s->seur_;
  193. if (i == luku) {
  194. return s->arvo_;
  195.  
  196. }
  197. s = tmp;
  198. luku++;
  199. }
  200. }
  201. const int & IntLista::operator[](size_type i) const {
  202. IntLista::size_type luku = 0;
  203. IntSolmu *s = paa();
  204. while (true) {
  205. IntSolmu *tmp = s->seur_;
  206. if (i == luku) {
  207. return s->arvo_;
  208. }
  209. s = tmp;
  210. luku++;
  211. }
  212. }
  213. IntLista::operator vector<int>() const {
  214. IntSolmu *s = this->paa_;
  215. vector<int> palauta;
  216. while (s != NULL) {
  217. IntSolmu *tmp = s->seur();
  218. if (s != NULL) {
  219. palauta.push_back(s->arvo());
  220. }
  221.  
  222. s = tmp;
  223. }
  224. return palauta;
  225. }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement