#include <cstdlib> #include <iostream> #include <conio.h> using namespace std; struct elem { int dane; elem *nast; }; void push(elem *&stos, int x); int pop(elem *&stos); int topEl(elem *stos); bool isEmpty(elem *stos); void usun(elem *&stos); int count(elem *stos); int main(int argc, char *argv[]) { unsigned int option = 0; int liczba = 0; elem *stos = NULL; while(option!=7) { cout << "\n\n\n\n1. Polozenie elementu na wierzcholku stosu " << endl; cout << "2. Pobranie ostatnio odlozonego elementu i zwrocenie go jako wartosci funkcji " << endl; cout << "3. Zwrocenie elementu znajdujacego sie na wierzcholku stosu bez jego usuwania " << endl; cout << "4. Sprawdzenie czy stos jest pusty " << endl; cout << "5. Usun stos " << endl; cout << "6. Zlicz elementy na stosie " << endl; cout << "7. Koniec programu " << endl; cout << "\n\n\nWybierz opcje " << endl; cin >> option; switch (option) { case 1: cout << "Podaj dowolna liczbe" << endl; cin >> liczba; push(stos, liczba); cout << "Polozono element na wierzcholek stosu" << endl; break; case 2: cout << "Pobrany element ze szczytu stosu " << pop(stos) << endl; break; case 3: cout << "Pobrany element z wierzcholku stosu bez usuwania " << topEl(stos) << endl; break; case 4: if (isEmpty(stos)) cout << "stos jest pusty" << endl; else cout << " stos nie jest pusty" << endl; break; case 5: usun(stos); cout << "Usunieto caly stos" << endl; break; case 6: cout << "Ilosc elementow w stosie: " << count(stos) << endl; break; } } return 0; } //================================================ void push(elem *&stos, int x) { elem *e = new elem; e->dane = x; e->nast = stos; stos = e; } //================================================ int pop(elem *&stos) { elem *e; if (stos == NULL) { cout <<"pusty stos" << endl; return -121; } int zwr = stos->dane; e = stos->nast; delete stos; stos = e; return zwr; } //================================================ int topEl(elem *stos) { if (stos == NULL) { cout <<"pusty stos" << endl; return -121; } int zwr = stos->dane; return zwr; } //================================================ bool isEmpty(elem *stos) { if (stos == NULL) { return true; } else return false; } //================================================ void usun(elem *&stos) { elem *e; while(stos) { e = stos->nast; delete stos; stos = e; } } //================================================ int count(elem *stos) { if (stos == NULL) return 0; int il = 0; while(stos) { stos = stos->nast; ++il; } return il; }