Created
July 27, 2016 15:39
-
-
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:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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