Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* HEADER */
- #ifndef OTE_INTLISTA_H
- #define OTE_INTLISTA_H
- #include <vector>
- #include <iostream>
- namespace otecpp_intlista
- {
- class IntSolmu
- {
- friend class IntLista;
- int arvo_;
- IntSolmu *seur_;
- public:
- IntSolmu(int arvo = 0, IntSolmu *seur = NULL);
- int arvo() const;
- IntSolmu* seur() const;
- void seur(IntSolmu *s);
- };
- std::ostream & operator<<(std::ostream &virta, const IntSolmu *s);
- class IntLista
- {
- public:
- typedef size_t size_type;
- private:
- IntSolmu *paa_;
- size_type koko_;
- public:
- IntLista();
- IntLista(const std::vector<int> &t);
- ~IntLista();
- IntLista(const IntLista &il2);
- IntLista & operator=(const IntLista &il2);
- void lisaaEteen(int arvo);
- void poistaEdesta();
- IntSolmu * paa() const;
- size_type koko() const;
- int & operator[](size_type i);
- const int & operator[](size_type i) const;
- operator std::vector<int>() const;
- };
- }
- #endif
- /* CPP */
- #include "intlista.h"
- using namespace std;
- namespace otecpp_intlista {
- IntSolmu * IntLista::paa() const {
- return IntLista::paa_;
- }
- IntLista::size_type IntLista::koko() const
- {
- return koko_;
- }
- void IntLista::poistaEdesta() {
- if (koko_ > 0)
- {
- IntSolmu * tmp = paa_;
- paa_ = paa_->seur();
- delete tmp;
- koko_ -= 1;
- }
- }
- void IntLista::lisaaEteen(int arvo) {
- paa_ = new IntSolmu(arvo, paa_);
- koko_ += 1;
- }
- IntLista & IntLista::operator=(const IntLista &il2) {
- if (this != &il2)
- {
- IntSolmu *s = paa_;
- while (s != NULL)
- {
- IntSolmu *tmp = s->seur();
- delete s;
- s = tmp;
- }
- koko_ = 0;
- if (il2.koko_ > 0)
- {
- paa_ = new IntSolmu(il2.paa_->arvo(), NULL);
- IntSolmu *s = paa_;
- koko_ = 1;
- for (IntSolmu * s2 = il2.paa_->seur(); s2 != NULL; s2 = s2->seur())
- {
- IntSolmu *tmp = new IntSolmu(s2->arvo(), NULL);
- s->seur(tmp);
- s = tmp;
- koko_ += 1;
- }
- }
- }
- return *this;
- }
- IntLista::IntLista(const IntLista &il2) : paa_(NULL), koko_(0)
- {
- if (il2.koko_ > 0)
- {
- paa_ = new IntSolmu(il2.paa_->arvo(), NULL);
- IntSolmu *s = paa_;
- koko_ = 1;
- for (IntSolmu * s2 = il2.paa_->seur(); s2 != NULL; s2 = s2->seur())
- {
- IntSolmu *tmp = new IntSolmu(s2->arvo(), NULL);
- s->seur(tmp);
- s = tmp;
- koko_ += 1;
- }
- }
- }
- IntLista::IntLista()
- : paa_(NULL), koko_(0)
- {
- }
- IntLista::~IntLista() {
- IntSolmu *s = paa_;
- while (s != NULL)
- {
- IntSolmu *tmp = s->seur();
- delete s;
- s = tmp;
- }
- }
- IntLista::IntLista(const vector<int> &t) {
- koko_ = 0;
- paa_ = NULL;
- if (t.size() > 0) {
- for (int i = t.size() - 1; i >= 0; i--) {
- this->lisaaEteen(t[i]);
- }
- }
- }
- IntSolmu::IntSolmu(int arvo, IntSolmu *seur)
- : arvo_(arvo), seur_(seur)
- {
- }
- int IntSolmu::arvo() const
- {
- return arvo_;
- }
- IntSolmu* IntSolmu::seur() const
- {
- return seur_;
- }
- void IntSolmu::seur(IntSolmu *s)
- {
- seur_ = s;
- }
- ostream &operator<<(ostream &virta, const IntLista &lista) {
- int luku = 0;
- IntSolmu *s = lista.paa();
- while (s != NULL) {
- IntSolmu *tmp = s->seur();
- if (luku == 0) {
- virta << s->arvo();
- luku = 1;
- }
- else {
- virta << " " << s->arvo();
- }
- s = tmp;
- }
- return virta;
- }
- ostream & operator<<(ostream &virta, const IntSolmu *s) {
- if (s != NULL) {
- virta << s->arvo();
- }
- else {
- virta << "NULL";
- }
- return virta;
- }
- int & IntLista::operator[](size_type i) {
- IntLista::size_type luku = 0;
- IntSolmu *s = paa_;
- while (true) {
- IntSolmu *tmp = s->seur_;
- if (i == luku) {
- return s->arvo_;
- }
- s = tmp;
- luku++;
- }
- }
- const int & IntLista::operator[](size_type i) const {
- IntLista::size_type luku = 0;
- IntSolmu *s = paa();
- while (true) {
- IntSolmu *tmp = s->seur_;
- if (i == luku) {
- return s->arvo_;
- }
- s = tmp;
- luku++;
- }
- }
- IntLista::operator vector<int>() const {
- IntSolmu *s = this->paa_;
- vector<int> palauta;
- while (s != NULL) {
- IntSolmu *tmp = s->seur();
- if (s != NULL) {
- palauta.push_back(s->arvo());
- }
- s = tmp;
- }
- return palauta;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement