Advertisement
Mary_99

help

Oct 19th, 2019
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. #define STACKSIZE 20
  5.  
  6. using namespace std;
  7.  
  8. class Stack{
  9.    
  10.     int totalSize;
  11.     int top;
  12.     int * array;
  13.    
  14.     public:
  15.     void push(int a);
  16.     int pop();
  17.     Stack(int size);
  18.     ~Stack();
  19.     bool isEmpty();
  20.     bool isFull();
  21.     int getSize();
  22.    
  23. };      
  24.        
  25. Stack::Stack(int size){
  26.     this->totalSize = size;
  27.     this->top = 0;
  28.     this->array = new int[this->totalSize]; //create the dynamic array
  29. }
  30.  
  31. Stack::~Stack(){
  32.     free(this-> array);
  33. }
  34.        
  35. bool Stack:: isEmpty(){
  36.     return this->top == false;
  37.    
  38. }
  39. bool Stack:: isFull(){
  40.     return this->top == this->totalSize -1;//-1
  41. }
  42.  
  43.  
  44. void Stack::push(int element){
  45.     if(isFull()){
  46.         this->array = (int*)malloc(sizeof(int)* this->totalSize * 2);
  47.         this->totalSize *= 2;
  48.     } else{  
  49.         this->array[this->top] = element;
  50.         this->top++;
  51.         }
  52. }
  53.    
  54. int Stack:: pop(){
  55.     if(isEmpty()){
  56.         cout << "Stack is empty" << endl;
  57.         abort();
  58.     }
  59.     else{
  60.         this->top--;
  61.         return this->array[this->top];
  62.     }
  63. }
  64.  
  65.  
  66. int main(int argc, char const *argv[])
  67. {
  68.     Stack * newStack = new Stack(STACKSIZE);
  69.    
  70.     Stack newStack
  71.    
  72.     newStack->push(5);
  73.     newStack->push(3);
  74.     newStack->push(15);
  75.    
  76.    // cout <<"Size of the stack: "; this->totalSize << endl;// chce tu sprawdzic czy sie tablica powiekszyłaXD
  77.  
  78.     newStack->push(35);
  79.     newStack->push(45);
  80.    // cout <<"Size of the stack: ", newStack->totalSize << endl;
  81.  
  82.     cout<< newStack->pop() << endl;
  83.  
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement