Guest User

Untitled

a guest
Dec 12th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. class Niz
  6. {
  7. vector<int>v;
  8. int trenutan;
  9. public:
  10. Niz()
  11. {
  12. trenutan=0;
  13. }
  14. void dodajElement(int el)
  15. {
  16. v.push_back(el);
  17. }
  18. int brojElemenata() const { return v.size(); }
  19. int trenutni()
  20. {
  21. if(v.size()==0) throw "Niz je prazan. ";
  22. return v[trenutan];
  23. }
  24. bool prethodni()
  25. {
  26. if(trenutan==0) return false;
  27. trenutan--;
  28. return true;
  29. }
  30. bool slijedeci()
  31. {
  32. if(trenutan==0) return false;
  33. trenutan++;
  34. return true;
  35. }
  36. void pocetak()
  37. {
  38. while(trenutan!=0)
  39. {
  40. trenutan--;
  41. }
  42. }
  43. void kraj()
  44. {
  45. while(trenutan!=v.size()-1)
  46. {
  47. trenutan++;
  48. }
  49. }
  50. void dodajIspred(const int &el)
  51. {
  52. vector<int>pomocni;
  53. if(v.size()==0)
  54. {
  55. v.push_back(el);
  56. trenutan=0;
  57. }
  58. else
  59. {
  60. for(int i=0;i<trenutan;i++)
  61. {
  62. pomocni.push_back(v[i]);
  63. }
  64. pomocni.push_back(el);
  65. for(int i=trenutan;i<v.size();i++)
  66. {
  67. pomocni.push_back(v[i]);
  68. }
  69. //vracanje u stari
  70. v.resize(pomocni.size());
  71. for(int j=0;j<pomocni.size();j++)
  72. {
  73. v[j]=pomocni[j];
  74. }
  75. trenutan=trenutan+1;
  76. }
  77. }
  78. void dodajIza(const int &el)
  79. {
  80. vector<int>pomocni;
  81. if(v.size()==0)
  82. {
  83. v.push_back(el);
  84. trenutan=0;
  85. }
  86. else
  87. {
  88. //dodavanje iza trenutni
  89. for(int i=0;i<=trenutan;i++)
  90. {
  91. pomocni.push_back(v[i]);
  92. }
  93. pomocni.push_back(el);
  94. for(int i=(trenutan+1);i<v.size();i++)
  95. {
  96. pomocni.push_back(v[i]);
  97. }
  98. //vracanje u stari
  99. v.resize(pomocni.size());
  100. for(int i=0;i<v.size();i++)
  101. {
  102. v[i]=pomocni[i];
  103. }
  104. }
  105. }
  106. void obrisi()
  107. {
  108. if(v.size()==0) throw "Niz je prazan. ";
  109. vector<int>pomocni;
  110. int x=trenutan;
  111. for(int i=0;i<v.size();i++)
  112. {
  113. if(i!=trenutan)
  114. {
  115. pomocni.push_back(v[i]);
  116. }
  117. }
  118. v.resize(pomocni.size());
  119. //vracanje u stari
  120. for(int i=0;i<pomocni.size();i++)
  121. {
  122. v[i]=pomocni[i];
  123. }
  124. if(trenutan==v.size()) trenutan=trenutan-1;
  125.  
  126. }
  127. void ispisiElemente() const
  128. {
  129. cout<<"Elementi su: ";
  130. for(int i=0;i<v.size();i++)
  131. {
  132. cout<<v[i]<<" ";
  133. }
  134. cout<<endl<<"trenutni element je : "<<trenutan;
  135. }
  136.  
  137. };
  138.  
  139.  
  140.  
  141.  
  142. int main()
  143. {
  144. Niz A;
  145.  
  146. A.dodajElement(5);
  147. A.dodajElement(4);
  148. A.dodajElement(3);
  149. A.zadajTrenutni(1);
  150. A.dodajIza(111);
  151. A.dodajIspred(555);
  152. A.kraj();
  153. A.obrisi();
  154.  
  155. A.ispisiElemente();
  156. cout<<endl<<endl;
  157. Niz B;
  158. B.dodajIspred(5);
  159. B.dodajIspred(111);
  160. B.ispisiElemente();
  161.  
  162.  
  163. return 0;
  164. }
Add Comment
Please, Sign In to add comment