#include #include #include #include #include using namespace std; /*—Zad. 1. Zaimplementuj podstawowe operacje stosowe — dołożenie elementu na wierzchołku stosu void push(ele* &stos, int x) — pobranie elementu ze stosu (ostatnio dołożonego) i zwrócenie go jako wartości funkcji int pop(ele* &stos) — zwrócenie elementu znajdującego sią na wierzchołku stosu bez jego usuwania int topEl(ele* stos) — sprawdzenie czy stos jest pusty bool isEmpty(ele* stos) }*/ struct element{ int liczba; element* nastepny; }; //ZAD 1 //a) Dodanie elementu na stos void push(element* &stos, int x) { cout << "Na stos wrzucam liczbe: " << x << endl; element*nowy = new element; nowy->liczba = x; nowy->nastepny = stos; stos = nowy; } //b) Zrzucenie elementu ze stosu int pop(element* &stos){ if (stos != 0) { int w = stos->liczba; element*wsk = stos; stos = stos->nastepny; delete wsk; cout << "Ze stosu sciagam liczbe: " << w << endl; return w; } else { cout << "Stos jest pusty" << endl; return 0; } } //c) Zwrócenie elementu ze stosu bez jego zrzucania int topEl(element* &stos){ if(stos!=0) { int w=stos-> liczba; cout << "Na szczycie stosu znajduje sie liczba: " << w << endl; return w; } else{ cout <<"Brak elementow"<< endl; return 0; } } //Sprawdź czy stos jest pusty bool isEmpty(element* &stos){ if(stos==0) { cout << "Stos jest pusty" << endl; return 1; } else { cout << "Na stosie sa elementy" << endl; return 0; } } void pokazMenu() { system("cls"); cout << "1. Dodaj element na stos \n" << "2. Zrzuc element ze stosu \n" << "3. Pokaz szczyt stosu \n" << "4. Sprawdz czy stos jest pusty \n" << "5. Wyjscie \n" << endl << "Wybor: "; } int main(int argc, char** argv) { int pobranaLiczba; int wybor; element *s1 = new element; s1 = 0; do { pokazMenu(); cin >> wybor; switch(wybor) { case 1: { cout << "Podaj liczbe ktora chcesz wrzucic na stos: "; cin >> pobranaLiczba; push (s1, pobranaLiczba); system("PAUSE"); break; } case 2: { pop(s1); system("PAUSE"); break; } case 3: { topEl(s1); system("PAUSE"); break; } case 4: { isEmpty(s1); system("PAUSE"); break; } } }while (wybor != 5); }