Skip to content

Instantly share code, notes, and snippets.

@ravikiran0606
Created July 27, 2016 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ravikiran0606/524dc83e46db41e17a49a25ff425ac4b to your computer and use it in GitHub Desktop.
Save ravikiran0606/524dc83e46db41e17a49a25ff425ac4b to your computer and use it in GitHub Desktop.
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