SHARE
TWEET

Untitled

a guest May 15th, 2015 213 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Stack
  2. {
  3. private:
  4.     int *p;
  5.     int top,length;
  6.  
  7. public:
  8.     Stack(int = 0);
  9.     ~Stack();
  10.  
  11.     void push(int);
  12.     int pop();
  13.     void display();
  14. };
  15.  
  16. Stack::Stack(int size)
  17. {
  18.     top=-1;
  19.     length=size;
  20.     if(size == 0)
  21.         p = 0;
  22.     else
  23.         p=new int[length];
  24. }
  25.  
  26. Stack::~Stack()
  27. {
  28.     if(p!=0)
  29.         delete [] p;
  30. }
  31.  
  32. void Stack::push(int elem)
  33. {
  34.     if(p == 0)                //If the stack size is zero, allow user to mention it at runtime
  35.     {
  36.        
  37.        
  38.     }
  39.     if(top==(length-1))     //If the top reaches to the maximum stack size
  40.     {
  41.         //<<"\nCannot push "<<elem<<", Stack full"<<endl;
  42.         return;
  43.     }
  44.     else
  45.     {
  46.         top++;
  47.         p[top]=elem;
  48.     }
  49. }
  50. int Stack::pop()
  51. {
  52.     if(p==0 || top==-1)
  53.     {
  54.         //<<"Stack empty!";
  55.         return -1;
  56.     }
  57.     int ret=p[top];
  58.     top--;
  59.     length--;
  60.  
  61.     return ret;
  62. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top