Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Stos{
- public:
- int *tab, wierzch, rozmiar;
- Stos(int rozmiar){
- cout << "Wywolano konstruktor 1-parametrowy z rozmiarem " << rozmiar << " o adresie: " << &tab << endl;
- }
- Stos(){
- cout << "Wywolano konstruktor domyslny o adresie: " << &tab << endl;
- }
- void init(Stos& s, int rozm){
- s.tab = new int[rozm];
- s.rozmiar = rozm;
- s.wierzch = -1;
- }
- void destroy(Stos& s){delete[] s.tab;}
- void push(Stos& s, int l){s.tab[++s.wierzch] = l;}
- void pop(Stos& s){--s.wierzch;}
- int top(Stos& s){return s.tab[s.wierzch];}
- int empty(Stos& s){return s.wierzch == -1;}
- int full(Stos& s){return s.wierzch == s.rozmiar -1;}
- ~Stos(){
- delete []tab;
- // cout << "Wywolano destruktor" << endl;
- cout << "Wywolano destruktor dla adresu: " << &tab << endl;
- } //destruktor
- }; //koniec klasy
- void dodaj(Stos& s, int a) {
- s.push(s,a);
- }
- int main()
- {
- Stos s1(3);
- Stos s2;
- s1.init(s1,10);
- s2.init(s2,1000);
- s1.push(s1,1);
- s1.push(s1,2);
- s1.push(s1,3);
- while(!s1.empty(s1)){
- cout << s1.top(s1) << endl;
- s1.pop(s1);
- }
- s1.destroy(s1);
- s2.destroy(s2);
- /*
- for(int i = 0; i < 1; i++){
- Stos s3(3);
- cout << "dziala";
- s3.push(s3,1);
- cout << s3.top(s3);
- }
- */
- /*
- Stos s;
- s.init(s,10); //wywolana przeze mnie metoda aby kod zadzialal
- s.push(s,0);
- dodaj(s,1);
- dodaj(s,2);
- while (!s.empty(s)) {
- cout << s.top(s) << endl;
- s.pop(s);
- } // bledy w dzialaniu powoduje to, ze stos s jest niezainicjowany (dodano przeze mnie metode init dla obiektu s)
- */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement