jupiterbjy

C++ stack attempt

Mar 25th, 2021 (edited)
863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Stack{
  6. private:
  7.     int *stored_values = new int[32] {20};
  8.     int current_cursor = 0;
  9.  
  10. public:
  11.     int pop(){
  12.         if (current_cursor) {
  13.             current_cursor--;
  14.  
  15.             cout << "Popping value: " << stored_values[current_cursor] << endl;
  16.  
  17.             int value = stored_values[current_cursor];
  18.             delete &(stored_values[current_cursor]);
  19.  
  20.             print_array();
  21.             return value;
  22.         }
  23.         return -1;
  24.     };
  25.  
  26.     void push(int val){
  27.         cout << "Pushing value: " << val << endl;
  28.         stored_values[current_cursor] = val;
  29.         current_cursor++;
  30.  
  31.         print_array();
  32.     };
  33.  
  34.     void print_array(){
  35.         cout << "Saved Data: [";
  36.  
  37.         for(int idx = 0; idx < 32; idx++)
  38.             cout << stored_values[idx] << ",";
  39.  
  40.         cout << "]" << endl;
  41.     }
  42. };
  43.  
  44.  
  45. int main() {
  46.     Stack reference_;
  47.     reference_.push(12);
  48.     reference_.pop();
  49. }
Add Comment
Please, Sign In to add comment