document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <iostream>
  2. #define capacity 1000000
  3.  
  4. using namespace std;
  5.  
  6. class Stack
  7. {
  8.     int * s;
  9.     int top;
  10.    
  11.     bool isEmpty ()
  12.     {
  13.         if (top == -1 || !s)
  14.             return true;
  15.         return false;
  16.     }
  17.    
  18.     bool isFull ()
  19.     {
  20.         if (top == capacity - 1)
  21.             return true;
  22.         return false;
  23.     }
  24.    
  25.     void pushAtBottom (int);
  26.    
  27.     public:
  28.         Stack ()
  29.         {
  30.             top = -1;
  31.             s = new int [capacity];
  32.         }
  33.        
  34.         ~Stack ()
  35.         {
  36.             delete s;
  37.         }
  38.        
  39.         void push (int);
  40.         int pop ();
  41.         void reverse ();
  42.         void print ();
  43. };
  44.  
  45. void Stack :: push (int data)
  46. {
  47.     if (isFull ())
  48.     {
  49.         cout << "Error: Stack overflow" << endl;
  50.         return;
  51.     }
  52.     s[++top] = data;
  53. }
  54.  
  55. int Stack :: pop ()
  56. {
  57.     if (isEmpty ())
  58.     {
  59.         cout << "Stack underflow" << endl;
  60.         return -1;
  61.     }
  62.     return s[top--];
  63. }
  64.  
  65. void Stack :: reverse ()
  66. {
  67.     if (isEmpty ())
  68.         return;
  69.     int data = pop ();
  70.     reverse ();
  71.     pushAtBottom (data);
  72. }
  73.  
  74. void Stack :: pushAtBottom (int data)
  75. {
  76.     if (isEmpty ())
  77.     {
  78.         push (data);
  79.         return;
  80.     }
  81.     int next = pop ();
  82.     pushAtBottom (data);
  83.     push (next);
  84. }
  85.  
  86. void Stack :: print ()
  87. {
  88.     for (int i = top; i >= 0; i--)
  89.         cout << s[i] << " ";
  90.     cout << endl;
  91. }
  92.  
  93. int main (void)
  94. {
  95.     Stack s;
  96.     s.push (1);
  97.     s.push (2);
  98.     s.push (3);
  99.     s.push (4);
  100.     s.push (5);
  101.     s.push (6);
  102.     s.push (7);
  103.    
  104.     s.print ();
  105.    
  106.     s.reverse ();
  107.    
  108.     s.print ();
  109.    
  110.     return 0;
  111. }
');