WAP of stack using structure and that needs to perform push & pop operation

/*WAP of stack using structure and that needs to perform push & pop operation*/
#include<iostream.h>
#include<conio.h>
//using namespace std;
void stack();
void push(int);
int pop();
struct stack
{
//private:
int arr[5],top;
//public:
}s;
void stack()
{
s.top=-1;
}
void push(int v)
{
if(s.top==4)
cout<<"Stack is full"<<endl;
else
{
s.arr[++s.top]=v;
cout<<"Data pushed successfully"<<endl;
}
}
int pop()
{
if(s.top==-1)
{
cout<<"Stack is empty"<<endl;
return NULL;
}
else
return s.arr[s.top--];
}
main()
{
///stack s;
int ch,k;
stack();
cout<<"1 for push\n2for pop";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter Variable";
cin>>k;
push(k);
break;
case 2:
cout<<pop();
break;
}
getch();
}

No comments:

Post a Comment