A Stack is a linear data structure which is used to store data in a particular order. Two operations that can be performed on a Stack are: Push operation which inserts an element into the stack. Pop operation which removes the last element that was added into the stack. It follows Last In First Out(LIFO) Order. Today I have just studied about Stack and thought I should share a program of Stack here with you all.
#include < iostream.h >
#include < stdlib.h >
#include < stdio.h >
class IntStack
{
public:
IntStack(int num) { top = 0; maxelem = num; s = new int[maxelem]; }
void push(int t)
{
if (top == maxelem) return;
s[top++] = t;
}
int pop()
{
if (top == 0) return -1;
return s[--top];
}
void display()
{
if (top == 0) { cout push(2);
s->display();
s->push(3);
s->display();
s->push(4);
s->display();
s->pop();
s->display();
s->pop();
s->display();
s->push(10);
s->display();
s->pop();
s->display();
s->pop();
s->display();
s->pop();
s->display();
s->pop();
s->display();
s->pop();
s->display();
}