- class Stack
- {
- private:
- int *p;
- int top,length;
- public:
- Stack(int = 0);
- ~Stack();
- void push(int);
- int pop();
- void display();
- };
- Stack::Stack(int size)
- {
- top=-1;
- length=size;
- if(size == 0)
- p = 0;
- else
- p=new int[length];
- }
- Stack::~Stack()
- {
- if(p!=0)
- delete [] p;
- }
- void Stack::push(int elem)
- {
- if(p == 0) //If the stack size is zero, allow user to mention it at runtime
- {
- }
- if(top==(length-1)) //If the top reaches to the maximum stack size
- {
- //<<"\nCannot push "<<elem<<", Stack full"<<endl;
- return;
- }
- else
- {
- top++;
- p[top]=elem;
- }
- }
- int Stack::pop()
- {
- if(p==0 || top==-1)
- {
- //<<"Stack empty!";
- return -1;
- }
- int ret=p[top];
- top--;
- length--;
- return ret;
- }
SHARE
TWEET

Untitled




Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
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.