stack_cd.cpp
Upload User: dq031136
Upload Date: 2022-08-08
Package Size: 802k
Code Size: 1k
Development Platform:

C++ Builder

  1. #include <iostream.h>
  2. #define ARR_SIZE 100
  3. class stack 
  4. {
  5.    int stck[ARR_SIZE];
  6.    int stack_top;
  7.  public:
  8.   stack();
  9.    ~stack();
  10.    void push(int i);
  11.    int pop();
  12.  };
  13. stack::stack(void)
  14.  {
  15.   stack_top = 0;
  16.    cout << "Stack Initialized" << endl;
  17.  }
  18. stack::~stack(void)
  19.  {
  20.   cout << "Stack Destroyed" << endl;
  21.  }
  22. void stack::push(int i)
  23.  {
  24.   if (stack_top==ARR_SIZE)
  25.     {
  26.      cout << "Stack is full." << endl;
  27.       return;
  28.     }
  29.    stck[stack_top] = i;
  30.    stack_top++;
  31.  }
  32. int stack::pop(void)
  33.  {
  34.   if (stack_top==0)
  35.     {
  36.      cout << "Stack underflow." << endl;
  37.       return 0;
  38.     }
  39.    stack_top--;
  40.    return stck[stack_top];
  41.  }
  42. void main(void)
  43.  {
  44.   stack obj1, obj2;
  45.    obj1.push(1);
  46.    obj2.push(2);
  47.    obj1.push(3);
  48.    obj2.push(4);
  49.    cout << obj1.pop() << endl;
  50.    cout << obj1.pop() << endl;
  51.    cout << obj2.pop() << endl;
  52.    cout << obj2.pop() << endl;
  53.  }
  54.