Advertisement
Mary_99

task1b in progress

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