Facebook
From Edgy Bat, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 277
  1. #include <iostream>
  2. #include <time.h>
  3. #include <cstdlib>
  4. #include <iomanip>
  5. #include <stdlib.h>
  6. using namespace std;
  7.  
  8.  
  9. /*—Zad.       1.      Zaimplementuj   podstawowe      operacje        stosowe
  10. — dołożenie elementu        na      wierzchołku    stosu  
  11. void push(ele* &stos, int x)
  12. — pobranie    elementu        ze      stosu   (ostatnio       dołożonego)   i       zwrócenie      go      jako    wartości       funkcji
  13. int pop(ele* &stos)
  14. — zwrócenie  elementu        znajdującego   sią    na      wierzchołku    stosu   bez     jego    usuwania       
  15. int topEl(ele* stos)
  16. — sprawdzenie czy     stos    jest    pusty  
  17. bool isEmpty(ele* stos)
  18. }*/
  19.  
  20.  
  21. struct element{
  22. int liczba;
  23. element* nastepny;
  24. };
  25. //ZAD 1
  26. //a) Dodanie elementu na stos
  27. void push(element* &stos, int x)
  28. {
  29.         cout << "Na stos wrzucam liczbe: " << x << endl;
  30.     element*nowy = new element;
  31.     nowy->liczba = x;
  32.     nowy->nastepny = stos;
  33.     stos = nowy;
  34. }
  35.  
  36. //b) Zrzucenie elementu ze stosu
  37. int pop(element* &stos){
  38.         if (stos != 0)
  39.         {
  40.     int w = stos->liczba;
  41.     element*wsk = stos;
  42.     stos = stos->nastepny;
  43.     delete wsk;
  44.     cout << "Ze stosu sciagam liczbe: " << w << endl;
  45.     return w;
  46.         }
  47.         else
  48.         {
  49.         cout << "Stos jest pusty" << endl;
  50.         return 0;
  51.         }
  52. }
  53.  
  54. //c) Zwrócenie elementu ze stosu bez jego zrzucania
  55. int topEl(element* &stos){
  56.     if(stos!=0)
  57.     {
  58.         int w=stos-> liczba;
  59.         cout << "Na szczycie stosu znajduje sie liczba: " << w << endl;
  60.         return w;
  61.     }
  62.     else{
  63.         cout <<"Brak elementow"<< endl;
  64.         return 0;
  65.     }
  66. }
  67.  
  68. //Sprawdź czy stos jest pusty
  69. bool isEmpty(element* &stos){
  70.     if(stos==0)
  71.         {
  72.         cout << "Stos jest pusty" << endl;
  73.         return 1;
  74.         }
  75.         else
  76.         {
  77.         cout << "Na stosie sa elementy" << endl;
  78.         return 0;
  79.         }
  80. }
  81.  
  82.  
  83.  
  84.  
  85. void pokazMenu()
  86. {
  87.         system("cls");
  88.         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: ";
  89. }
  90.  
  91.  
  92.  
  93. int main(int argc, char** argv) {
  94.  
  95. int pobranaLiczba;
  96. int wybor;
  97.  
  98. element *s1 = new element;
  99. s1 = 0;
  100.  
  101.  
  102.  
  103.  
  104. do
  105. {
  106. pokazMenu();
  107. cin >> wybor;  
  108. switch(wybor)
  109. {
  110.         case 1:
  111.         {
  112.                 cout << "Podaj liczbe ktora chcesz wrzucic na stos: ";
  113.                 cin >> pobranaLiczba;
  114.                 push (s1, pobranaLiczba);
  115.                 system("PAUSE");
  116.                 break;
  117.         }
  118.         case 2:
  119.         {
  120.                 pop(s1);
  121.                 system("PAUSE");
  122.                 break;
  123.                        
  124.         }
  125.         case 3:
  126.         {
  127.         topEl(s1);
  128.         system("PAUSE");
  129.         break;
  130.         }
  131.         case 4:
  132.         {      
  133.         isEmpty(s1);
  134.         system("PAUSE");
  135.         break;
  136.         }      
  137. }
  138. }while (wybor != 5);
  139.  
  140. }
  141.