stack

Sry i make stack (becouse i wanna lern how it's working) can u check this ?
And say something about it

template <typename T>
class Element {
public:
T wartosc;
Element<T> * nast;
Element<T> * poprz;
Element(T wartosc) {
nast = NULL;
this->wartosc = wartosc;
}
};


template <typename T>
class Kolejka {
Element<T> * glowa;
Element<T> * ogon;
int rozmiar;
public:
Kolejka() {
glowa = NULL;
ogon = NULL;
rozmiar = 0;
}
void push(T wartosc) {
Element<T> * element = new Element<T>(wartosc);
element->poprz = ogon;
if (ogon) {
ogon->nast = element;
ogon = element;
} else {
glowa = ogon = element;
}
rozmiar++;
}
T pop() {
assert(glowa);
T wynik = glowa->wartosc;
Element<T> * usun = glowa;
if (glowa->nast) {
glowa->nast->poprz = NULL;
glowa = glowa->nast;
if (glowa->nast == NULL){
ogon = glowa;
}
} else {
glowa = ogon = NULL;
}
delete usun;
rozmiar--;
return wynik;
}
int size() {
return rozmiar;
}
};
Usually people here ask for help with specific problems.
Do add a main() and test your own code. If you come across any issue, we’ll be more than happy to help you.
Topic archived. No new replies allowed.