Advertisement
Paszta

Stos

Jun 7th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3.  
  4. using namespace std;
  5.  
  6. struct stos
  7. {
  8.     int data;
  9.     stos* next;
  10. };
  11.  
  12. bool is_Empty(stos *st);
  13. void push (stos* &st, int new_data);
  14. void pop(stos* &st);
  15. void take_element(stos* st);
  16.  
  17.  
  18. int main()
  19. {
  20.     int w;
  21.     stos* st = nullptr;
  22.     st = new stos[10];
  23.     srand(time(NULL));
  24.     //st = 0;
  25.     while (w!=5){
  26.     cout << " 1 - sprawdzenie czy stos jest pusty" << endl;
  27.     cout << " 2 - dodanie elementu na stos" << endl;
  28.     cout << " 3 - usuniecie ze stosu" << endl;
  29.     cout << " 4 - pobranie elementu ze stosu" << endl;
  30.     cout << " 5 - wyjscie z programu" << endl;
  31.     cout << " Co chcesz zrobic?" << endl;
  32.     cin >> w;
  33.  
  34.     switch (w)
  35.     {
  36.     case 1: is_Empty(st); break;
  37.     case 2: push (st,rand()); break;
  38.     case 3: pop(st); break;
  39.     case 4: take_element(st); break;
  40.     case 5:  cout << "juz nic nie mozesz zrobic " << endl; break;
  41.     default: cout << " Bledny wybor" << endl;
  42.     }
  43.     }
  44.     return 0;
  45. }
  46.  
  47. bool is_Empty(stos* st){
  48.     if(st == 0) cout << " Stos jest pusty" << endl;
  49.     else{
  50.     cout << " W stosie cos jednak bylo" << endl;
  51.    return st == nullptr;
  52.     }
  53. }
  54.  
  55. void push (stos* &st, int new_data){
  56.     cout << new_data << endl;
  57. stos* dodana_wartosc = new stos;
  58. dodana_wartosc -> data = new_data;
  59. dodana_wartosc -> next = st;
  60. st = dodana_wartosc;
  61. }
  62.  
  63. void pop(stos* &st){
  64.     if(st != 0){
  65.         stos* temp = st;
  66.         st = st -> next;
  67.         delete temp;
  68.         cout << "Usuwanie powiodlo sie" << endl;
  69.         }
  70.     else
  71.         cout << "Usuwanie nie powiodlo sie, stos jest pusty" << endl;
  72. }
  73.  
  74. void take_element(stos* st){
  75. if (st == 0)
  76.     cout << "stos jest pusty" << endl;
  77. else{
  78.     cout << "na gorze stosu jest liczba :" << st -> data << endl;
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement