Facebook
From Chartreuse Frog, 6 Years ago, written in C++.
This paste is a reply to Re: Re: Re: Untitled from Torrid Flamingo - view diff
Embed
Download Paste or View Raw
Hits: 269
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. struct element{
  9.     int liczba;
  10.     element *next;
  11. };
  12.  
  13. bool czyPusty(element *stos){
  14.     bool wynik = stos == nullptr ? true : false;
  15.     return wynik;
  16. }
  17. void dodajElement(element*& stos){
  18.     srand(time(nullptr));
  19.  
  20.     element* el = new element;
  21.     el->liczba = rand()%100 +0;
  22.     el->next = stos;
  23.     stos = el;
  24.  
  25. }
  26. void usunElement(element*& stos){
  27.     if(stos != nullptr){
  28.         element* temp = stos;
  29.         stos = stos->next;
  30.         delete temp;
  31.     }
  32.  
  33. }
  34. int pobierzElement(element* stos){
  35.     if(stos != nullptr){
  36.         return stos->liczba;
  37.     }else{
  38.         return -1;
  39.     }
  40. }
  41.  
  42. void usunWszystkieElementy(element*& stos){
  43.     while(!czyPusty(stos)){
  44.         usunElement(stos);
  45.     }
  46. }
  47.  
  48. int main()
  49. {
  50.     cout << "Menu:" << "n"
  51.         << "1 - sprawdzenie czy stos jest pusty" << "n"
  52.         << "2 - dodanie elementu na stos" << "n"
  53.         << "3 - usuniecie elementu ze stosu" << "n"
  54.         << "4 - pobranie elementu ze stosu" << "n"
  55.         << "5 - usunięcie wszystkich elementów ze stosu" << "n"
  56.         << "6 - wyjście z programu" << "n"
  57.         << endl;
  58.  
  59.     element* stosZNullPnt = nullptr;
  60.  
  61.     int menu = -1;
  62.     while(menu != 6){
  63.         cin >> menu;
  64.         switch (menu){
  65.         case 1:
  66.             {
  67.             czyPusty(stosZNullPnt);
  68.             string komunikat = czyPusty(stosZNullPnt) ? "Pusty" : "Niepusty";
  69.             cout << komunikat << endl;
  70.             }
  71.             break;
  72.         case 2:
  73.             dodajElement(stosZNullPnt);
  74.             break;
  75.         case 3:
  76.             usunElement(stosZNullPnt);
  77.             break;
  78.         case 4:
  79.             {
  80.             int ostatni = pobierzElement(stosZNullPnt);
  81.             cout << "Ostatni element to: " << ostatni << endl;
  82.             break;
  83.             }
  84.         case 5:
  85.             usunWszystkieElementy(stosZNullPnt);
  86.             break;
  87.         case 6:
  88.             return 0;
  89.         }
  90.     }
  91.     return -1;
  92. }
  93.  
captcha