Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- class Niz
- {
- vector<int>v;
- int trenutan;
- public:
- Niz()
- {
- trenutan=0;
- }
- void dodajElement(int el)
- {
- v.push_back(el);
- }
- int brojElemenata() const { return v.size(); }
- int trenutni()
- {
- if(v.size()==0) throw "Niz je prazan. ";
- return v[trenutan];
- }
- bool prethodni()
- {
- if(trenutan==0) return false;
- trenutan--;
- return true;
- }
- bool slijedeci()
- {
- if(trenutan==0) return false;
- trenutan++;
- return true;
- }
- void pocetak()
- {
- while(trenutan!=0)
- {
- trenutan--;
- }
- }
- void kraj()
- {
- while(trenutan!=v.size()-1)
- {
- trenutan++;
- }
- }
- void dodajIspred(const int &el)
- {
- vector<int>pomocni;
- if(v.size()==0)
- {
- v.push_back(el);
- trenutan=0;
- }
- else
- {
- for(int i=0;i<trenutan;i++)
- {
- pomocni.push_back(v[i]);
- }
- pomocni.push_back(el);
- for(int i=trenutan;i<v.size();i++)
- {
- pomocni.push_back(v[i]);
- }
- //vracanje u stari
- v.resize(pomocni.size());
- for(int j=0;j<pomocni.size();j++)
- {
- v[j]=pomocni[j];
- }
- trenutan=trenutan+1;
- }
- }
- void dodajIza(const int &el)
- {
- vector<int>pomocni;
- if(v.size()==0)
- {
- v.push_back(el);
- trenutan=0;
- }
- else
- {
- //dodavanje iza trenutni
- for(int i=0;i<=trenutan;i++)
- {
- pomocni.push_back(v[i]);
- }
- pomocni.push_back(el);
- for(int i=(trenutan+1);i<v.size();i++)
- {
- pomocni.push_back(v[i]);
- }
- //vracanje u stari
- v.resize(pomocni.size());
- for(int i=0;i<v.size();i++)
- {
- v[i]=pomocni[i];
- }
- }
- }
- void obrisi()
- {
- if(v.size()==0) throw "Niz je prazan. ";
- vector<int>pomocni;
- int x=trenutan;
- for(int i=0;i<v.size();i++)
- {
- if(i!=trenutan)
- {
- pomocni.push_back(v[i]);
- }
- }
- v.resize(pomocni.size());
- //vracanje u stari
- for(int i=0;i<pomocni.size();i++)
- {
- v[i]=pomocni[i];
- }
- if(trenutan==v.size()) trenutan=trenutan-1;
- }
- void ispisiElemente() const
- {
- cout<<"Elementi su: ";
- for(int i=0;i<v.size();i++)
- {
- cout<<v[i]<<" ";
- }
- cout<<endl<<"trenutni element je : "<<trenutan;
- }
- };
- int main()
- {
- Niz A;
- A.dodajElement(5);
- A.dodajElement(4);
- A.dodajElement(3);
- A.zadajTrenutni(1);
- A.dodajIza(111);
- A.dodajIspred(555);
- A.kraj();
- A.obrisi();
- A.ispisiElemente();
- cout<<endl<<endl;
- Niz B;
- B.dodajIspred(5);
- B.dodajIspred(111);
- B.ispisiElemente();
- return 0;
- }
Add Comment
Please, Sign In to add comment