#include <iostream>
#include<algorithm>
template<typename Tip>
class Stek{
struct Cvor{
Tip elementi;
Cvor *veza;
};
Cvor *vrh;
void Test(){if(!vrh) throw std::logic_error("Prazan stek!");}
void Dealociraj();
void Kopiraj(const Stek &s);
public:
Stek () : veza(nullptr){}
~Stek(){Dealociraj()}
Stek(const Stek &s) : vrh(Kopiraj(s)) {}
Stek(Stek &&s) : vrh(s.vrh) {s.vrh = nullptr;}
Stek &operator =(const Stek &s);
Stek &operator =(Stek &&s) {std::swap(vrh, s.vrh); return *this;}
void DodajNaVrh(const Tip &element){vrh = new Cvor{element, vrh};}
const Stek &DajVrh() const {Test(); return vrh->element; }
Stek &DajVrh() {Test(); return vrh->element; }
int DajVelicinu() const;
bool DaLiJePrazan() const {return !vrh;}
void SkiniSVrha();
};
template<typename Tip>
int Stek<Tip>::DajVelicinu() const{
int brojac = 0;
for(Cvor *p = vrh; p != nullptr; p = p->veza) brojac++;
return brojac;
}
template <typename Tip>
void Stek<Tip>::SkiniSVrha(){
if(!vrh) return;
Cvor * stari = vrh;
vrh = vrh->veza;
delete stari;
}
template <typename Tip>
void Stek<Tip>::Dealociraj(){
if(!vrh) return;
while(!DaLiJePrazan) SkiniSVrha();
}
template <typename Tip>
void Stek<Tip>::Kopiraj(const Stek &s){
Cvor *vrh = nullptr;
Cvor *prethodni;
try{
for(Cvor *p = vrh; p != nullptr; p = p->veza){
Cvor *novi = new Cvor{tekuci->element, nullptr};
if(!vrh) vrh = novi;
else prethodni->veza = novi;
prethodni = novi;
}
} catch(...){
Dealociraj();
throw;
}
return vrh;
}
template <typename Tip>
Stek<Tip> &Stek<Tip>::operator =(const Stek &s){
if(&s != this){
Cvor *novi_vrh = Kopiraj(s);
Dealociraj();
vrh = novi_vrh;
}
return *this;
}
int main() {
std::cout << "Hello world!" << std::endl;
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: