Advertisement
Karolina99

Array stack

Nov 18th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class AStos
  6. {
  7. private:
  8.     int* items; //tablica przechowujaca elementy stosu
  9.     int capacity; //max pojemnosc tablicy
  10.     int sp; //wskaznik na szczyt stosu, stack pointer
  11. public:
  12.     AStos(int c); //konstruktor
  13.     bool empty(); //zwraca true, jesli stos jest pusty, w przeciwnym razie zwraca false
  14.     bool full(); //zwraca true, jesli stos jest pelny, w przeciwnym razie zwraca false
  15.     void push(int x); //dodaje element na szczyt stosu
  16.     int pop();      //usuwa element ze szczytu stosu i zwraca daną ze stosu
  17.     void clear(); //zwalnia pamiec, czyli usuwa cala tablice dynamiczna przechowujaca elementy stosu
  18.     friend ostream& operator<<(ostream& out, AStos& s);
  19. };
  20.  
  21. AStos::AStos(int c) //jako parametr przyjmuje c, czyli max pojemnosc tablicy dynamicznej
  22. {
  23.     items = new int[c];
  24.     capacity = c;
  25.     sp = 0; //jezeli stos jest pusty sp przyjmuje indeks pierwszej komorki tablicy, jesli jest pelny to wskazuje indeks komorki tuz nad szczytem stosu
  26. }
  27.  
  28. bool AStos::empty()
  29. {
  30.     /**if (sp == 0)
  31.     {
  32.         return true;
  33.     }
  34.     else
  35.     {
  36.         return false;
  37.     }**/
  38.  
  39.     if (sp == 0)
  40.         return true;
  41.     return false;
  42. }
  43.  
  44. bool AStos::full()
  45. {
  46.     /**if (sp == capacity - 1)
  47.     {
  48.         return true;
  49.     }
  50.     else
  51.     {
  52.         return false;
  53.     }**/
  54.  
  55.     if (sp == capacity)
  56.         return true;
  57.     return false;
  58. }
  59.  
  60. void AStos::push(int x)
  61. {
  62.     if (!full())
  63.     {
  64.         items[sp] = x;
  65.         sp++;
  66.     }
  67.     else
  68.     {
  69.         //cout << "Stos jest pelny!" << endl;
  70.         return;
  71.     }
  72. }
  73.  
  74. int AStos::pop()
  75. {
  76.     if (!empty())
  77.     {
  78.         sp--;
  79.         int wartosc;
  80.         wartosc = items[sp];
  81.         return wartosc;
  82.     }
  83.     else
  84.     {
  85.         //cout << "Stos jest pusty!" << endl;
  86.         return NULL;
  87.     }
  88. }
  89.  
  90. void AStos::clear()
  91. {
  92.     if (!empty())
  93.     {
  94.         sp = 0;
  95.     }
  96.     else
  97.     {
  98.         //cout << "Stos jest pusty!" << endl;
  99.         return;
  100.     }
  101. }
  102.  
  103. ostream& operator<<(ostream& out, AStos& s)
  104. {
  105.     int temp = s.sp - 1;
  106.     while (temp >= 0)
  107.     {
  108.         out << s.items[temp] << "  ";
  109.         temp--;
  110.     }
  111.     return out;
  112. }
  113.  
  114. /**driver code**/
  115. int main()
  116. {
  117.     //AStos s(10);
  118.     //drugi sposob
  119.     AStos s = AStos(10);
  120.  
  121.     s.push(1);
  122.     s.push(2);
  123.     s.push(3);
  124.     s.push(4);
  125.     cout << s << endl;
  126.  
  127.     s.pop();
  128.     cout << s << endl;
  129.  
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement