/*ALGORITHM:bth insertion nd deletion is at one end only
insertion:-frst check if array is empty to enter element nd if yes thn enter
deletion:-frst check if array has somethng to delte if yes decrease top by 1
*/
#include<iostream>
#include<conio.h>
using namespace std;
const int size=5;
class stack
{
	int a[size];
	int top;			//used to traverse in array nd store topmost elemnt
	public:
		stack()
		{
			top=-1;		//array indexing starts from 0 thus initialisong top=-1 implies array to be empty
		}
		void ins();
		void del();
		void display();
};
void stack::ins()
{
	int ele;
	if(top==size-1)		//to check whether array is already full(if true no space to insert)
	{
		cout<<"\nOVERFLOW ERROR..."<<endl;
		return;
	}
	else
	{
		cout<<"ENTER ELEMENT";
		cin>>ele;
		a[++top]=ele;  		//++top beacuzz initialy top=-1 nd to enter element frst elemnt at index no 0 v frst increase thn write
	}
}
void stack::del()
{
	if(top==-1)   		//to check whther array is empty(no element wud b there to delete)
	{
		cout<<"\nUNDERFLOW ERROR..."<<endl;
		return;
	}
	else
	{
		cout<<a[top]<<" deleted.."<<endl;
		top--;       //top is decremented implies now the elemnt at current index=top is nt prt of array whn nxt tym insert func wud b called it wud be overwrited 
	}
}
void stack::display()
{
	for(int i=top;i>=top;i--)   
	{
		cout<<a[i]<<"  ";
	}
		
}
int main()
{
	int choice;
	char ans;
	stack s;
	do
	{
		cout<<"MENU:"<<endl
		<<"1.INSERT"<<endl
		<<"2.DELETE"<<endl
		<<"3.DISPLAY"<<endl
		<<"4.EXIT"<<endl<<endl;
	cout<<"ENTER YOUR CHOICE:";
	cin>>choice;
	switch(choice)
	{
		case 1:
			s.ins();
			break;
		case 2:
			s.del();
			break;
		case 3:
			s.display();
			break;
		case 4:
			exit(0);
	}
	cout<<"\nDO YOU WANT TO GO BACK TO MAIN MENU:";
	cin>>ans;
	}while(ans!='n');
	
	getch();
	return 0;
}