Advertisement
Mary_99

task 2b

Oct 27th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <assert.h>
  3. #include <string.h>
  4. #include "Stack2b.h"
  5. #define STACKSIZE 3
  6. #define INIT -1
  7.  
  8. using namespace std;
  9.  
  10. Stack::Stack()
  11. {
  12.     totalSize = STACKSIZE;
  13.     top = INIT;
  14.     array = (int*)malloc(sizeof(int)*totalSize);
  15.     if(array == NULL)
  16.     {
  17.         printf("Memory not allocated\n");
  18.         abort();
  19.     }
  20.  
  21. }
  22.  
  23.  
  24. Stack::Stack(const Stack &obj)
  25. {
  26.     top = obj.top;
  27.     totalSize = obj.totalSize;
  28.     array = (int*)malloc(sizeof(int) * totalSize);
  29.     assert(array != NULL);
  30.     for(int i=0; i<obj.totalSize; i++)
  31.     {
  32.         array[i] = obj.array[i];
  33.     }
  34. }
  35.  
  36.  
  37. Stack& Stack::operator= (const Stack &obj)
  38. {
  39.     top = obj.top;
  40.     totalSize = obj.totalSize;
  41.     array = (int*)realloc(array, sizeof(int) * totalSize);
  42.     assert(array != NULL);
  43.     for(int i=0; i<obj.totalSize; i++)
  44.     {
  45.         array[i] = obj.array[i];
  46.     }
  47.     return *this;
  48. }
  49.  
  50.  
  51. void Stack::push(int element)
  52. {
  53.     if(top == totalSize - 1)
  54.     {
  55.         totalSize *= 2;
  56.         int *temp = (int*)realloc(array, sizeof(int)*totalSize);
  57.         if(temp == NULL)
  58.         {
  59.            cout<<"Realloc failed"<<endl;
  60.             abort();
  61.         } else {
  62.             array = temp;
  63.         }
  64.     }
  65.     array[++top] = element;
  66. }
  67.  
  68. int Stack::pop()
  69. {
  70.     if(isEmpty())
  71.     {
  72.         cout<<"Stack is empty"<<endl;
  73.         abort();
  74.     }
  75.     return array[top--];
  76. }
  77.  
  78. bool Stack::isEmpty()
  79. {
  80.     if(top == -1)
  81.     {
  82.         return true;
  83.     } else {
  84.         return false;
  85.     }
  86. }
  87.  
  88. void Stack::getTotatSize()
  89. {
  90.     printf("Size of stack is: %d\n", totalSize);
  91. }
  92.  
  93. Stack::~Stack()
  94. {
  95.    free(array);
  96.  
  97. }
  98. void Stack::display(){
  99.     int i;
  100.     for(i = 0; i <= top; i++)
  101.         cout << array[i]<<endl;
  102. }
  103.  
  104.  
  105. #include <iostream>
  106. #include <string>
  107. #include <string.h>
  108. #include "Stack2b.h"
  109. #include "Stack2b.cpp"
  110.    
  111. #define STACKSIZE 3
  112. #define DOUBLE_SIZE 2
  113. #define INITIAL_TOP -1  
  114.    
  115. using namespace std;
  116.  
  117.  
  118. int main(int argc, char const *argv[])
  119. {  
  120.     Stack s1;
  121.     s1.push(1);
  122.     s1.push(2);
  123.     s1.push(3);
  124.    
  125.    
  126.     Stack s2(s1);
  127.  
  128.     Stack s3 = s1;
  129.  
  130.     Stack s4;
  131.     s4.push(10);
  132.     s4 = s1;
  133.    
  134.     s1.display();
  135.     printf("\n");
  136.     s2.display();
  137.     printf("\n");
  138.     s3.display();
  139.     printf("\n");
  140.     s4.display();
  141.    
  142.     return 0;
  143. }
  144. #ifndef STACK_H
  145. #define STACK_H
  146.  
  147. class Stack{
  148.    
  149.     int totalSize;
  150.     int top;
  151.     int * array;
  152.    
  153.     public:
  154.     void display();
  155.     void push(int elemennt);
  156.     int pop();
  157.     bool isEmpty();
  158.     void getTotatSize();
  159.     Stack(const Stack &obj);
  160.     Stack& operator= (const Stack &obj);
  161.     Stack();
  162.     ~Stack();
  163.    
  164. };  
  165.  
  166. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement