Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class stack{
  5. private:
  6.     int size, *stack_, count;
  7. public:
  8.     stack(int);
  9.     ~stack(){
  10.     };
  11.     stack push(const int&n);
  12.     bool empty();
  13.     bool full();
  14.     stack pop();
  15.     void print();
  16.     void clear();
  17. };
  18. stack::stack(int n = 0){
  19.     size = n;
  20.     stack_ = new int[size];
  21. }
  22. stack stack::push(const int&n){
  23.     if (full())
  24.     {
  25.         cout << "stack is full, can not push" << endl;
  26.     }
  27.     else
  28.         stack_[count++] = n;
  29.     return *this;
  30. }
  31. bool stack::empty(){
  32.     if (count < 0){
  33.         return true;
  34.     }
  35.     else return false;
  36. }
  37. bool stack::full(){
  38.     if (count >= size){
  39.         return true;
  40.     }
  41.     else return false;
  42. }
  43. stack stack::pop(){
  44.     if (empty())
  45.     {
  46.         count = 0;
  47.         cout << "stack is empty, can not pop" << endl;
  48.     }
  49.     else
  50.         count--;
  51.     return *this;
  52. }
  53. void stack::print(){
  54.     cout << "---------" << endl;
  55.     for (int i = 0; i < count; i++){
  56.         cout << stack_[i] << endl;
  57.     }
  58.     cout << "---------" << endl;
  59. }
  60. void stack::clear(){
  61.     delete stack_;
  62. }
  63.  
  64. int main(){
  65.     stack p(5);
  66.     p.push(1);
  67.     p.push(1);
  68.     p.push(1);
  69.     p.push(1);
  70.     p.push(1);
  71.     p.push(1);
  72.     p.push(1);
  73.     p.print();
  74.     p.pop();
  75.     p.print();
  76.     p.clear();
  77.     system("pause");
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement