#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 << "\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; } } getch(); 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 = 1; elem *e = stos; while(e) { e = stos->nast; ++il; } RETURN il; }