Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <class t>
- class Stack
- {
- private:
- t * arr;
- int top;
- int siz;
- void Reserve()
- {
- t *temp = arr;
- int sz=siz;
- arr = new t[siz*2];
- siz*=2;
- top=-1;
- for(int i=0; i<sz; i++)
- push(temp[i]);
- }
- public:
- Stack()
- {
- siz=10;
- arr = new t[siz];
- top=-1;
- }
- Stack(Stack &s)
- {
- siz=s.siz;
- arr = new int[s.siz];
- for(int i=0; i<siz; i++)
- arr[i] = s.arr[i];
- top=s.top;
- siz=s.siz;
- }
- ~Stack()
- {
- delete arr;
- }
- void push(t x)
- {
- if(isFull())
- Reserve();
- top++;
- arr[top]=x;
- }
- void pop(t &x)
- {
- if(isEmpty())
- {
- cout<<"Stack is already empty"<<endl;
- throw;
- }
- x=arr[top];
- top--;
- }
- t pop()
- {
- if(isEmpty())
- {
- cout<<"Stack is already empty"<<endl;
- throw;
- }
- int temp=top;
- top--;
- return arr[temp];
- }
- bool isFull()
- {
- if(top==siz-1) return 1;
- return 0;
- }
- bool isEmpty()
- {
- if(top==-1) return 1;
- return 0;
- }
- void peek()
- {
- cout<<"top "<<arr[top]<<endl;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement