Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1. template <class t>
  2. class Stack
  3. {
  4. private:
  5.     t * arr;
  6.     int top;
  7.     int siz;
  8.     void Reserve()
  9.     {
  10.         t *temp = arr;
  11.         int sz=siz;
  12.         arr = new t[siz*2];
  13.         siz*=2;
  14.         top=-1;
  15.         for(int i=0; i<sz; i++)
  16.             push(temp[i]);
  17.     }
  18. public:
  19.     Stack()
  20.     {
  21.         siz=10;
  22.         arr = new t[siz];
  23.         top=-1;
  24.     }
  25.     Stack(Stack &s)
  26.     {
  27.         siz=s.siz;
  28.         arr = new int[s.siz];
  29.         for(int i=0; i<siz; i++)
  30.             arr[i] = s.arr[i];
  31.         top=s.top;
  32.         siz=s.siz;
  33.     }
  34.     ~Stack()
  35.     {
  36.         delete arr;
  37.     }
  38.     void push(t x)
  39.     {
  40.         if(isFull())
  41.             Reserve();
  42.         top++;
  43.         arr[top]=x;
  44.     }
  45.     void pop(t &x)
  46.     {
  47.         if(isEmpty())
  48.         {
  49.             cout<<"Stack is already empty"<<endl;
  50.             throw;
  51.         }
  52.         x=arr[top];
  53.         top--;
  54.     }
  55.     t pop()
  56.     {
  57.         if(isEmpty())
  58.         {
  59.             cout<<"Stack is already empty"<<endl;
  60.             throw;
  61.         }
  62.         int temp=top;
  63.         top--;
  64.         return arr[temp];
  65.     }
  66.     bool isFull()
  67.     {
  68.         if(top==siz-1) return 1;
  69.         return 0;
  70.     }
  71.     bool isEmpty()
  72.     {
  73.         if(top==-1) return 1;
  74.         return 0;
  75.     }
  76.     void peek()
  77.     {
  78.        cout<<"top "<<arr[top]<<endl;
  79.     }
  80. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement