Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C++ program to implement a Stack class with structure variables as its attributes:
#include<iostream>
#define maxi 1000
using namespace std;
struct strstack{
int a[maxi];
int top;
};
class sstack{
strstack s;
public:
void initialize();
void push(int val);
void pop();
void peep();
void display();
};
void sstack::initialize(){
cout<<"Empty Stack is created :D ";
s.top=-1;
}
void sstack::push(int val){
if(s.top==maxi-1){
cout<<"\nThe Stack is already full...";
cout<<"\nThe Stack is already full...";
}
s.a[++s.top]=val;
}
void sstack::pop(){
if(s.top==-1){
cout<<"\nThe Stack is already empty..";
}
else{
cout<<"\nThe Popped element is.."<<s.a[s.top];
s.top--;
}
}
void sstack::peep(){
if(s.top==-1){
cout<<"\nThe Stack is already empty..";
}
else{
cout<<"\nThe Topmost element is.."<<s.a[s.top];
}
}
void sstack::display(){
if(s.top==-1){
cout<<"\nThe Stack is Empty..";
}
else{
cout<<"\nThe Contents of the Stack is..";
int x=s.top;
while(x!=-1){
cout<<s.a[x]<<" ";
x--;
}
}
}
int main()
{
sstack s;
int ch,x,val;
s.initialize();
cout<<"\nChoice : \n1) Push \n2) Pop \n3) Peep \n4) Display \n5) Exit :P";
while(1){
cout<<"\nEnter your choice..";
cin>>ch;
if(ch==1){
cout<<"\nEnter the element to be inserted..";
cin>>val;
s.push(val);
}
else if(ch==2){
s.pop();
}
else if(ch==3){
s.peep();
}
else if(ch==4){
s.display();
}
else break;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment