Advertisement
Deriterath

stos

Feb 9th, 2020
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class stos
  6. {
  7. private:
  8.     struct node
  9.     {
  10.         int element; //wartosc elementu
  11.         node *prev; //wskaznik na poprzednika
  12.     };
  13.     unsigned int i; //stan
  14.     node *last, *help; //pomoc - wsk na pierwszy z góry
  15. public:
  16.     stos();
  17.     stos(stos const &);
  18.     ~stos();
  19.     unsigned int rozmiar(); //liczba elementow na stosie
  20.     void poloz(int);
  21.     int zdejmij();
  22.     friend std::ostream& operator<<(std::ostream&, stos&);
  23. };
  24.  
  25. stos::stos()
  26. {
  27.     i = 0;
  28.     last = NULL;
  29.     help = NULL;
  30. }
  31.  
  32. stos::stos(stos const & S)
  33. {
  34.     //dodatkowa zmienna pomocnicza ze wzgledu na brak mozliwosci edycji klasy
  35.     node *SHelp;
  36.     SHelp = S.last;
  37.     int j = 0;
  38.     int *elements = new int[S.i];
  39.  
  40.     //czytam kopie stosu
  41.     while (SHelp != NULL)
  42.     {
  43.         elements[j] = SHelp->element;
  44.         SHelp = SHelp->prev;
  45.         j++;
  46.     }
  47.  
  48.     //wpisywanie elementow starego stosu - chcac miec te same stosy, czytam tablice elementow odwrotnie
  49.     for (j = S.i-1; j >= 0; j--)
  50.     {
  51.         if (j == 0)
  52.         {
  53.             last = new node;
  54.             last->element = elements[j];
  55.             last->prev = help;
  56.         }
  57.  
  58.         else
  59.         {
  60.             help = new node;
  61.             help->element = elements[j];
  62.             help->prev = (j == S.i - 1) ? NULL : last;
  63.             last = help;
  64.         }
  65.     }
  66.     i = S.i;
  67. }
  68.  
  69. stos::~stos()
  70. {
  71.     while (last != NULL)
  72.     {
  73.         help = last->prev;
  74.         delete last;
  75.         last = help;
  76.     }
  77. }
  78.  
  79. unsigned int stos::rozmiar()
  80. {
  81.     return i;
  82. }
  83.  
  84. void stos::poloz(int x)
  85. {
  86.     if (i == 0)
  87.     {
  88.         last = new node;
  89.         last->element = x;
  90.         last->prev = NULL;
  91.     }
  92.     else
  93.     {
  94.         help = new node;
  95.         help->element = x;
  96.         help->prev = last;
  97.         last = help;
  98.     }
  99.     i++;
  100. }
  101.  
  102. int stos::zdejmij()
  103. {
  104.     if (last == NULL) return 0;
  105.     int popValue = last->element;
  106.  
  107.     help = last->prev;
  108.     delete last;
  109.     last = help;
  110.     i--;
  111.  
  112.     return popValue;
  113. }
  114.  
  115. std::ostream & operator<<(std::ostream &stream, stos  &S)
  116. {
  117.     S.help = S.last;
  118.     while (S.help != NULL)
  119.     {
  120.         stream << S.help->element << std::endl;
  121.         S.help = S.help->prev;
  122.     }
  123.     return stream;
  124. }
  125.  
  126. int main()
  127. {
  128.     stos stosik;
  129.  
  130.     stosik.poloz(5);
  131.     stosik.poloz(218);
  132.     stosik.poloz(-3);
  133.     stosik.poloz(133);
  134.     stosik.poloz(30);
  135.  
  136.     cout << stosik << endl;
  137.     cout << "Rozmiar stosu: " << stosik.rozmiar() << endl << endl;
  138.  
  139.     stos stosikKopia(stosik);
  140.  
  141.     stosik.zdejmij();
  142.     stosik.zdejmij();
  143.  
  144.     cout << stosik << endl;
  145.  
  146.     cout << "Rozmiar stosu: " << stosik.rozmiar() << endl << endl;
  147.  
  148.     return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement