Advertisement
Mary_99

2b kopia

Nov 3rd, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 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.    
  44.     for(int i=0; i<obj.top; i++)
  45.     {
  46.         array[i] = obj.array[i];
  47.     }
  48.     return *this;
  49. }
  50.  
  51. void Stack::copy(const Stack &obj)
  52. {
  53.     top = obj.top;
  54.    
  55.     for(int i=0; i<obj.top; i++)
  56.     {
  57.         array[i] = obj.array[i];
  58.     }
  59. }
  60.  
  61. void Stack::push(int element)
  62. {
  63.     if(top == totalSize - 1)
  64.     {
  65.         totalSize *= 2;
  66.         int *temp = (int*)realloc(array, sizeof(int)*totalSize);
  67.         if(temp == NULL)
  68.         {
  69.            cout<<"Realloc failed"<<endl;
  70.             abort();
  71.         } else {
  72.             array = temp;
  73.         }
  74.     }
  75.     array[++top] = element;
  76. }
  77.  
  78. int Stack::pop()
  79. {
  80.     if(isEmpty())
  81.     {
  82.         cout<<"Stack is empty"<<endl;
  83.         abort();
  84.     }
  85.     return array[top--];
  86. }
  87.  
  88. bool Stack::isEmpty()
  89. {
  90.     if(top == -1)
  91.     {
  92.         return true;
  93.     } else {
  94.         return false;
  95.     }
  96. }
  97.  
  98. void Stack::getTotatSize()
  99. {
  100.     printf("Size of stack is: %d\n", totalSize);
  101. }
  102.  
  103. Stack::~Stack()
  104. {
  105.    free(array);
  106.  
  107. }
  108. void Stack::display(){
  109.     int i;
  110.     for(i = 0; i <= top; i++)
  111.         cout << array[i]<<endl;
  112. }
  113. int Stack::giveTop()
  114. {
  115.     return array[top];
  116. }
  117.  
  118. #include <iostream>
  119. #include <string>
  120. #include <string.h>
  121. #include "stack2b.h"
  122. #include "stack2b.cpp"
  123.    
  124. #define STACKSIZE 3
  125. #define DOUBLE_SIZE 2
  126. #define INITIAL_TOP -1  
  127.    
  128. using namespace std;
  129.  
  130.  
  131. int main(int argc, char const *argv[])
  132. {  
  133.     Stack s;
  134.     assert(s.isEmpty()==1);
  135.     s.push(1);
  136.     s.push(2);
  137.     s.push(3);
  138.     s.display();
  139.     cout<<endl;
  140.     assert(s.pop()==3);
  141.     s.display();
  142.     cout<<endl;
  143.    
  144.    
  145.     Stack s2(s);
  146.     assert(s2 ==s);
  147.    
  148.     s2.display();
  149.     cout<<endl;
  150.     s.push(3);
  151.     s.push(4);
  152.    
  153.     //assert(s2.giveTop()==4);
  154.     assert(s2.isEmpty()==0);
  155.     assert(s2.pop()==4);
  156.     s2.display();
  157.     cout<<endl;
  158.    
  159.     s=s2;
  160.    
  161.    
  162.  
  163.     s2.display();
  164.     cout<<endl;
  165.     s.display();
  166.    
  167.  
  168.    
  169.     return 0;
  170. }
  171. #pragma once
  172.  
  173. class Stack{
  174.    
  175.     int totalSize;
  176.     int top;
  177.     int * array;
  178.    
  179.     public:
  180.     void display();
  181.     void push(int elemennt);
  182.     int pop();
  183.     bool isEmpty();
  184.     int giveTop();
  185.     void getTotatSize();
  186.     void copy(const Stack &obj);
  187.     Stack(const Stack &obj);
  188.     Stack& operator= (const Stack &obj);
  189.     Stack();
  190.     ~Stack();
  191.    
  192. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement